Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

object

events

Is there a way to create an event that 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.

like image 714
Sinaesthetic Avatar asked Nov 08 '10 09:11

Sinaesthetic


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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

like image 176
Thomas Levesque Avatar answered Nov 11 '22 21:11

Thomas Levesque


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.

like image 21
wh1t3cat1k Avatar answered Nov 11 '22 20:11

wh1t3cat1k