Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 709545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:30:54+00:00 2026-05-14T04:30:54+00:00

How would you share the same object between two other objects? For instance, I’d

  • 0

How would you share the same object between two other objects? For instance, I’d like something in that flavor:

class A
{
   private string foo_; // It could be any other class/struct too (Vector3, Matrix...)

   public A (string shared)
   {
       this.foo_ = shared;
   }

   public void Bar()
   {
       this.foo_ = "changed";
   }
}

...
// inside main
string str = "test";
A a = new A(str);

Console.WriteLine(str); // "test"
a.Bar();
Console.WriteLine(str); // I get "test" instead of "changed"... :(

Here, I don’t want to give a ref to the Bar method. What I want to achieve is something that would look like that in C++:

class A
{
  int* i;
public:
  A(int* val);
};

A::A (int* val)
{
  this->i = val;
}

I read there is some ref/out stuff, but I couldn’t get what I’m asking here. I could only apply some changes in the methods scope where I was using ref/out arguments…
I also read we could use pointers, but is there no other way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:30:54+00:00Added an answer on May 14, 2026 at 4:30 am

    This has nothing to do with sharing objects. You passed a reference to a string into the A constructor. That reference was copied into the private member foo_. Later, you called B(), which changed foo_ to “changed”.

    At no time did you modify str. str is a local variable in main. You never passed a reference to it.

    If you had wanted to change str, you could have defined B as

       public void Bar(ref string s)
       {
         this.foo_ = "changed";
         s = this.foo_;
       }
    

    Consider:

    public class C
    {
        public int Property {get;set;}
    }
    
    public class A
    {
        private C _c;
        public A(C c){_c = c;}
    
        public void ChangeC(int n) {_c.Property = n;}
    }
    
    public class B
    {
        private C _c;
        public B(C c){_c = c;}
    
        public void ChangeC(int n) {_c.Property = n;}
    }
    

    in main:

    C myC = new C() {Property = 1;}
    A myA = new A(myC);
    B myB = new B(myC);
    
    int i1 = myC.Property; // 1
    myA.ChangeC(2);
    int i2 = myC.Property; // 2
    myB.ChangeC(3);
    int i3 = myC.Property; // 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer is this possible? No. same origin policy? Yes. May 16, 2026 at 4:49 pm
  • Editorial Team
    Editorial Team added an answer There isn't things like this in C. A static array's… May 16, 2026 at 4:49 pm
  • Editorial Team
    Editorial Team added an answer Your main problem seems to be that you're returning the… May 16, 2026 at 4:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Think Design: I have many applications that share the same user database! Other tables
We have a large WebForms application here that we would like to port over
I need to build an encrypted connection between two peers, and I need to
I understand that every time I type the string literal , the same String
I'm looking for suggestions for ways to share Android app data between phones running
So I did some refactoring and two of my classes now look exactly the
What's a good pattern, if any, to share a piece of Core Data model
I have a problem that is not uncommon when building a plug-in architecture. Assembly
I have created two wsdl files with shared types imported from xsd schema file.
I have a situation in which the ideal relationship, I believe, would involve Value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.