Welcome to Rainbow Portal Community Sign in | Join | Help

Events and Delegates and the Observer

I am not sure why, but it took me a while to reallly grasp Events and Delegates. I understood what they where, and how to use them, but for some reason it took a little while to click in my head. So for anyone in the same boat I was, here is a very simple way of understanding them, while looking at a code sample.

Event - Something that Happened

   1:  public event System.EventHandler EventHappened;
   2:  protected virtual int OnEventHappened(System.EventArgs e)
   3:  {
   4:      if ((this.EventHappened != null))
   5:          this.EventHappened(this, e);
   6:  }

 

OnEventHappened is the event that you are offering your clients code to attach to. Meaning you are accustomed to seeing something like

   1:  basepage.OnEventHappened += new (.....);

 

Once this happens once, the compiler adds the events to the process train for that event. So EventHappened is no longer null, and the event will fire. and bubble up to anyone registered to that event. You are used to using this in things like DataGrid.OnItemDataBound which fires at the begining of the databind process for the grid.

Delegates can be thought of a little like EventHandlers...

   1:  public delegate void ItemChangedEventHandler(object sender, ItemChangedEventArgs e);
   2:  public class ItemChangedEventArgs : System.EventArgs
   3:  {
   4:      ......
   5:  }

 

So if you put them together, you end up with very useful conclusions.... Such as the classic Observer Pattern I have been dealing with a lot lately.

   1:  <PRE lang=cs>private int m_MyValue;
   2:   
   3:  public int MyValue
   4:  {
   5:      get
   6:      {
   7:          return this.m_MyValue;
   8:      }
   9:      set
  10:      {
  11:          if ((this.m_MyValue != value))
  12:          {
  13:              int oldValue = this.m_MyValue;
  14:              this.m_MyValue = value;
  15:              this.OnMyValueChanged(new MyValueChangedEventArgs(oldValue, 
  16:                                                          this.m_MyValue));
  17:          }
  18:      }
  19:  }
  20:   
  21:  public event MyValueChangedEventHandler MyValueChanged;
  22:   
  23:  protected virtual void OnMyValueChanged(MyValueChangedEventArgs e)
  24:  {
  25:      if ((this.MyValueChanged != null))
  26:      {
  27:          this.MyValueChanged(this, e);
  28:      }
  29:  }
  30:   
  31:  public delegate void MyValueChangedEventHandler(object sender, 
  32:                                         MyValueChangedEventArgs e);
  33:   
  34:  public class MyValueChangedEventArgs : System.EventArgs
  35:  {
  36:      private int m_OldValue;
  37:   
  38:      private int m_NewValue;
  39:   
  40:      public MyValueChangedEventArgs(int OldValue, int NewValue)
  41:      {
  42:          this.m_OldValue = OldValue;
  43:          this.m_NewValue = NewValue;
  44:      }
  45:   
  46:      public virtual int OldValue
  47:      {
  48:          get
  49:          {
  50:              return this.m_OldValue;
  51:          }
  52:      }
  53:   
  54:      public virtual int NewValue
  55:      {
  56:          get
  57:          {
  58:              return this.m_NewValue;
  59:          }
  60:      }
  61:  }</PRE>
Published lunedì 1 maggio 2006 19.13 by Jonathan
Filed under: ,

Comments

No Comments

Anonymous comments are disabled