Properties that "bubble up" set property

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


Answer this question

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

    Ahh, okay. Then no, you can't do what you want because the Something property is only called to get the something object, not to call InnerProperty. What is really happening under the covers:

    MyObject temp = myClass.Something;
    temp.InnerProperty = "value";

    You can't do anything in the Something property get/set to deal with InnerProperty changing. You'd have to keep track of the "parent" object and send it an event, something like this:

    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

    Ok thanks, I just wanted to make sure before I made all those events.

    Cheers,
    Aditya

  • anamika

    What Alex is saying is your Something property get will get called with the following code:


    MyClass.Something.InnerProperty = "value";
    Is that what you need it to do



  • Corrupt65805

    I know that it calls get to get MyClass.Something, but I want to do something in MyClass when MyClass.Something.InnerProperty is changed. How do I know in MyClass when a user changes that value if he's not actually setting MyClass.Something to a totally new object

    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.


  • Properties that "bubble up" set property