locked
Create a Event RRS feed

  • Question

  • have just finished a chapter on Events and Delegates. I am still unsure how to implement the following.

    Problem Statement : I want to make a class Sample, having a property MyString, which get a set a string mystr. Now this class will be instantiate in Main function of program class. Ex :

    public static void main()

    {

       Sample obj = new Sample();

       obj.MyString = "Try";

       Func1(obj);

    }

    Void Func1(Sample obj)

    {

       obj.MyString += "More";

    }

     

    I want to create an event OnChangemystr, which  will be raised whenever the value of

    string mystr is changed.

    How can i raise this kind of event. Please help.

    Sunday, April 29, 2007 3:09 PM

Answers

  •  

    1-) Define the EventArgs class that will hold your specific info...


    Code Snippet
    class MyStringChangedEventArgs : EventArgs
    {
    private string newValue;

    public MyStringChangedEventArgs(string newValue)


    {

    this.newValue = newValue;


    }

     

     

    public NewValue


    {

    get { return this.newValue; }


    }

     

    }

     

    2-) Define the delegate (signature) for the function that will handle such events (I prefer to use the existing EventHandler<T>, being:

    delegate (public delegate void EventHandler<TEventArgs> ( Object sender, TEventArgs e ) where TEventArgs : EventArgs

    3-) Define the event:


    Code Snippet
    event EventHandler<MyStringChangedEventArgs> MyStringChanged;

     

    4-) Write code to raise the vent:


    Code Snippet
    void OnMyStringChanged(string newValue)
    {
    // you may want to do a bit more research on concurrency issues, since this is not completely safe...
    if (this.MyStringChanged != null)
    {
    this.MyStringChanged(this, new MyStringChangedEventArgs(newValue));
    }
    }


    5-) Now we'll implement the setter of the MyString property so that it raises the event:


    Code Snippet
    public MyString
    {
    set {
    if (this.myString != value)
    {
    this.myString = value;
    this.OnMyStringChanged(value);
    }
    }


    6-) We subscribe to the event that is exposed by your Sample class:


    Code Snippet
    Sample sample = new Sample();
    sample.MyStringChanged += HandleMyStringChanged;

    void HandleMyStringChanged(object sender, MyStringChangedEventArgs e)
    {
    MessageBox.Show("MyString has changed to " + e.NewValue);
    }


    You may want to look at the INotifyPropertyChanged interface, because this is the one that you would need to implement to make databinding work.. And it's very similar to what you already have:


    Code Snippet
    class Sample
    {
     public event PropertyChangedEventHandler PropertyChanged;

     public String MyString
     {
      set
      {
       if (this.myString != value)
       {
        this.myString = value;
        this.OnPropertyChanged("MyString");
       }
      }
     }
     
     public int MyId
     {
      if (this.myId != value)
      {
       this.myId = value;
       this.OnPropertyChanged("MyId");
      }
     }

     private void OnPropertyChanged(string propertyName)
     {
      if (this.PropertyChanged != null)
      {
       this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
     }
    }
    }


    And if you databind eg a couple of labels to the Properties you will see that they update as soon as you change their values

    this.label1.DataBindings.Add("Text", mySample, "MyString");
    this.label2.DataBindings.Add("Text", mySample, "MyId");

    mySample.MyString = "something different"; // as soon as you do this, a PropertyChanged event will be raised, and databinding will make sure to update the UI

    Sunday, April 29, 2007 3:17 PM