How to keep a reference to a class property updated?
So lets say I have Class 1
public Class1
{
Class2 myClass2;
public Class1()
{
myClass2 = new Class2();
}
public SomeObject anObject
{
get;
set;
}
}
and Class2
public Class2
{
private SomeObject _myObject;
public SomeObject MyObject
{
get{ return _myObject;}
set{ _myObject = value; }
}
public void DoStuffToObject()
{
_myObject.Property = newValue;
}
}
How can I keep _myObject in Class2 the same as anObject in Class1 if I set anObject equal to a different开发者_StackOverflow object. So if I change anObject in Class1 _myObject is Class2 changes as well? Is this a good solution:
(in Class1)
private SomeObject _anObject;
public SomeObject anObject
{
get{ return _anObject; }
set{ _anObject = value;
if(myClass2 != null)
myClass2.MyObject = value;
}
}
It seems a little extraneous, but sense you can't pass a property by reference I am a little stumped.
Thanks
public Class1
{
private Class2 myClass2;
public Class1()
{
myClass2 = new Class2();
}
public SomeObject anObject
{
get { return myClass2.MyObject; }
set { myClass2.MyObject = value; }
}
}
精彩评论