tags:

views:

828

answers:

5

I have a property that is assigned as:

public string propertyA{get;set;}

I want to call a method automatically when this property is set to assign another property in the class. Best way to trigger the call?

+2  A: 

Expand out the property setter and assign the other property.

Jeff Martin
+13  A: 

You don't have to use that syntax that is just shorthand. If you expand it you can do whatever you like in the setter.

 public string PropertyA
 {
       get { return a; }
       set 
       {
            a = value;
            doStuff(); 
       }
 }
cgreeno
+2  A: 

Add the backing field manually and provide some code to do what you want in the settor.

private string propertyA;
public string PropertyA
{
    get { return this.propertyA; }
    set
    {
         this.propertyA = value;
         this.propertyB = value + "B";
    }
}
tvanfosson
+4  A: 

I think you'll have to go back to doing your property the old fashioned way.

private string _PropertyA;

public string propertyA
{
    get
    {
       return _PropertyA;
    }
    set
    {
       _PropertyA=value;
       //Set other parameter
    }
}
+1  A: 

Define the setter.

Inside it either trigger an event or directly assign the other property.

GoodEnough