开发者

c#: create an event for when an object's field values change

Is there a way to create an event tha开发者_JAVA技巧t will fire when an object's attributes/field values change? For instance, if the object has a field called

private int number;

And the user performs an action that would update that number, an event would fire that would update all of the text boxes on the form to display current field values?

EDIT: Sorry, yes properties have been created for each field.


Make it a property rather than a field, and implement INotifyPropertyChanged in your class :

class YourClass : INotifyPropertyChanged
{

    private int _number;
    public int Number
    {
        get { return _number; }
        private set
        {
            _number = value;
            OnPropertyChanged("Number");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

}

You can then listen explicitly for the PropertyChanged event, or use a data binding that will handle it automatically


You should use user-defined getters and setters (i.e. properties) to manually fire the event. Look at this code, it should be pretty simple:

// We declare a delegate to handle our event

delegate void StateChangedEventHandler(object sender, StateChangedEventArgs e);

// We declare event arguments class to specify, which value has changed to which value.

public class StateChangedEventArgs: EventArgs
{
    string propertyName;

    object oldValue;
    object newValue;

    /// <summary>
    /// This is a constructor.
    /// </summary>
    public StateChangedEventArgs(string propertyName, object oldValue, object newValue)
    {
        this.propertyName = propertyName;

        this.oldValue = oldValue;
        this.newValue = newValue;
    }
}

/// <summary>
/// Our class that we wish to notify of state changes.
/// </summary>
public class Widget
{
    private int x;

    public event StateChangedEventHandler StateChanged;

    // That is the user-defined property that fires the event manually;

    public int Widget_X
    {
        get { return x; }
        set
        {
            if (x != value)
            {
                int oldX = x;
                x = value;

                // The golden string which fires the event:
                if(StateChanged != null) StateChanged.Invoke(this, new StateChangedEventArgs("x", oldX, x);
            }
        }
    }
}

Our member variable is defined private, so no one outside of the class Widget can access it; they are forced to use the property Widget_X, and once they do, there are two cases:

  • They get the x variable. Nothing to do.
  • They set the x variable to the same value it had before. Nothing to do. We check it inside the setter.
  • They set the x variable and change it. That is where we fire the event.

It is critical to check if we have any event handlers registered (that is, our event is not null) before we ever invoke the event. In other case, we'll get an exception.


Sounds like you should check out the INotifyPropertyChanged interface. It doesn't work for fields though, and you'll have to implement it manually.


Where are you going to use this?

If you're going to use this in a client application then implementing INotifyPropertyChanged is the recommended way of working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜