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

Related Questions

I would like to share an object between various instances of objects of the
I would like to exchange objects between two python interpreters working on the same
Problem: I would like to share code between multiple assemblies. This shared code will
My problem is: I have 3 procs that would like to share config loaded
So I'm creating an array called fruits which I would like to share between
I would like to make a function that will create objects that have a
I was wondering how I would be able to share an observablecollection between two
I would like to share my Oracle SQL Developer configuration across my several computers
I am developing an app in which i would like to share my data
I would like to know if there is a method to create share buttons

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.