Reputation: 7128
In my application my main form is a singleton class MainForm.Instance
. It has a property, that has a property, etc. To save my fingers from typing it out in a class I am working on I have set a local reference to the property.
MyClass myClassInst = MainForm.Instance.MyProperty.AProperty.TheProperty;
With this local reference I do all of my work
myClassInst.AnInt = 4;
myClassInst.AThing = newValue;
// ... etc
Say I have now created a new instance of MyClass
and want to set the MainForm.Instance....
equal to it. Do I need to reset my local reference to the new object?
MyClass newClassInst = new MyClass();
MainForm.Instance.MyProperty.AProperty.TheProperty = newClassInst;
// Do I need to do this?
myClassInst = newClassInst;
// What about this?
myClassInst = MainForm.Instance.MyProperty.AProperty.TheProperty;
Also, did I use the right vocabulary when refering to what I call "my local reference"?
Now I "know" I do not need to do this, but I have thoroughly confused myself. myClassInst
should point to the memory location that holds a MyClass
object, no matter if the object itself changes myClassInst
should always refer to it as long as it exists, correct?
EDIT: I think I just found a much more eloquent way to solve my issue outlined above. As stated I have some local reference along the lines of
MyClass myClassInst = MainForm.Instance.MyProperty.AProperty.TheProperty;
But this is actually implemented as a property in my application
private MyClass MyClassInst { get; set; } = ...;
Instead of worrying about when to "reset" myClassInst
to point to a new reference I can just modify the property to return or modify the current reference of MainForm.Instance...
private MyClass MyClassInst
{
get { return MainForm.Instance.MyProperty.AProperty.TheProperty; }
set { MainForm.Instance.MyProperty.AProperty.TheProperty = value; }
}
This property will then return whatever TheProperty
currently references, and inturn will assign the current reference TheProperty
points to.
This reminds me of using
aliasing for class names...
using ShortName = ANameSpace.BNameSpace.AReallyRidiculouslyLongClassName;
Upvotes: 0
Views: 168
Reputation: 112259
myClassInst
holds the same reference as TheProperty
, i.e. they both point to the same object.
myClassInst -------------> Object
^
|
TheProperty -----------------+
If you assign a new object to TheProperty
, then myClassInst
still references the old object. Why should it point to the new object?
myClassInst -------------> Object
TheProperty -------------> new object
Note: neither myClassInst
nor TheProperty
are the object. They are references to an object, like arrows pointing to something.
But you could reference AProperty
instead.
MyAClass aProp = MainForm.Instance.MyProperty.AProperty;
aProp.TheProperty.AnInt = 4;
MyClass newClassInst = new MyClass{ AnInt = 5 };
MainForm.Instance.MyProperty.AProperty.TheProperty = newClassInst;
// Now, since AProperty didn't change, you can reference the new object
Console.WriteLine(aProp.TheProperty.AnInt); // Prints 5
Upvotes: 2
Reputation: 103437
Yes, you need to do one or the other of those two things.
Your myClassInst
is a copy of a reference to an instance. When you create a new instance, you need to update all references to point to the new instance.
Updates to the reference that you copied originally do not propagate to copies of that reference.
Upvotes: 1