Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>you can't store a reference to another variable in a class</p> </blockquote> <p>You can, people do it all the time. Simply assign it to a (private) field.</p> <blockquote> <p>so when I do new C(ref one); [snip /] later on after one is updated in A it's like C no longer knows that its still supposed to be pointing towards one </p> </blockquote> <p>Actually, it does know that, but you should assign <code>one</code> to a member field. Assigning to objects is nothing more then setting a reference to the object. Changing the object, changes them everywhere you've assigned it:</p> <pre><code>class A { public string Hello { get; set; } } class C { private A myA; public C(A a) { myA = a; } public A GetMyA() { return myA; } } // somewhere else: A someA = new A(); someA.Hello = "Hello World"; C someC = new C(someA); someA.Hello = "Other World"; Console.WriteLine(someC.GetMyA().Hello); // this will print "Other World" and not "Hello World" as you suggest </code></pre> <p><em>PS: since you were posting pseudocode, I hope you don't mind me simplifying it a bit. If I misunderstood your question, then please disregard (and perhaps clarify, if possible).</em></p> <p><em>PPS: rereading your (edited) code and still trying to figure out what the problem is, there seems to be nothing that prevents <code>C</code> in your code from keeping a member with a reference to <code>A</code> and calling <code>a.Add</code> will obviously reflect the member variable in <code>c</code> as well. Indeed, no need for <code>ref</code> here.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload