Link to home
Start Free TrialLog in
Avatar of auk_ie
auk_ie

asked on

Property Notification

Hello, Is there a way I can create a property in a class (eg. public int m_nNumber), and somehow create an event handler which is called each time this property is changed?
SOLUTION
Avatar of eternal_21
eternal_21

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of auk_ie
auk_ie

ASKER

Nice answer tzxie2000 (a little elaborate for my needs but very good)

eternal_21, How would you write a handler for the public event System.EventHandler NumberChanged;

I tried

public void NumberChanged(object sender, NumChangedEventArgs e)//event action
{
  MessageBox.Show(number.ToString());
}

But that doen't compile

just use System.EventArgs rather than NumChangedEventArgs

public void NumberChanged(object sender, System.EventArgs e)//event action
{
  MessageBox.Show(number.ToString());
}
Avatar of auk_ie

ASKER

Yes this is what I had,


public event System.EventHandler NumberChanged; //What does this line do.

//Duplicate declaration
public void NumberChanged(object sender, System.EventArgs e)//event action
{
  MessageBox.Show(number.ToString());
}
so, what compile error did you get ?
I think you'd better use the full version of event
or you may simplely write a function to call it

//define a function
void NumberChanged(int number)
{
 //doing something
}//if in a class you can overwrite it for different usage

 public int Number
  {
    get
    {
      return number;
    }
    set
    {
        number = value;
        NumberChanged(number);
    }
  }