I don't really know the right term for this, but the following is what I am looking for.
Say I have the following:
public class MyClass
{
private MyObject mSomething;
public MyObject Something
{
get
{
return mSomething;
}
set
{
mSomething = value;
}
}
}
Now say MyObject has an inner property MyObject.InnerProperty, and the user does MyClass.Something.InnerProperty = "value", is there any way for the set { } block for the Something property to be "called".
I am sorry the question is not so clear, but I am trying to do this without making events for every single property...
Any help is appreciated.
Thanks,
Aditya

Properties that "bubble up" set property
pcoulter
Here are my 2 cents...
In case like yours where I had to create properties which itself are other classes in my project, sometimes I had created read-only properties so that I could have control on when to create the internal object. C# 2.0 also allows you to change the access modifer for either get() or set(). Here's one version of the class:
public class MyClass
{
private MyObject mSomething = null;
public MyObject Something
{
get
{
//Intialize mSomething only once
if (mSomething == null)
mSomething = new MyObject();
return mSomething;
}
//C# 2.0 allows you to change modifiers on get or set
//accessors to be more restrictive than it's defination.
//Since Something property is public, I can use
//protected, internal or private
private set
{
mSomething = value;
}
}
}
Sometimes you also need callers to set the internal objects, in that case you can keep an internal flag and then take the necessay action if caller is setting the value to a separate instance. Here's another version I can think of:
public class MyClass
{
private MyObject mSomething;
public MyObject Something
{
get
{
return mSomething;
}
set
{
if (!mSomething.Equals(value))
{
//Take some action here caller is changing
//internal value of Something
}
mSomething = value;
}
}
}
Hope this helps.
pepa z depa
MyObject temp = myClass.Something;
temp.InnerProperty = "value";
sealed class Class1
{
public EventHandler InnerPropertyChanged;
public string InnerProperty
{
set
{
if(InnerPropertyChanged != null)
{
InnerPropertyChanged(this, EventArgs.Empty);
}
}
}
}
sealed class Class2
{
private Class1 something = new Class1();
private void something_InnerPropertyChanged ( object sender, EventArgs e )
{
// TODO:
}
public Class2 ( )
{
something.InnerPropertyChanged += new EventHandler(something_InnerPropertyChanged);
}
public Class1 Something
{
get
{
return this.something;
}
set
{
if (something != null)
{
something.InnerPropertyChanged -= new EventHandler(something_InnerPropertyChanged);
}
something = value;
if (something != null)
{
something.InnerPropertyChanged += new EventHandler(something_InnerPropertyChanged);
}
}
}
}
smmorrissey
Cheers,
Aditya
anamika
What Alex is saying is your Something property get will get called with the following code:
MyClass.Something.InnerProperty = "value";
Corrupt65805
Hope that's clearer.
Thanks,
Aditya
SPerkin
MyClass.Something.InnerProperty = "value";
This will work with your existing code without any changes, assuming that MyObject is reference type.