Subscribing to Events


Instead of declaring your own events, you will usually want to subscribe to events that another class has. In Web programming you will want to subscribe to events from the Page object, for example, events such as Load . Web controls also have events. For example, whenever you add a button, you will most likely want to subscribe to its click event. VS.NET has a wizard that automatically generates code to subscribe to an event, but it's good to know how to do it by hand in order to understand the subscription mechanism.

To subscribe to an event:

  1. To subscribe to an event, you must have a function in your class that has the same parameters as the delegate the event is based on. Add the function to receive the event notification. If the event is based on EventHandler as most events aresimply add a function like this:

     private void ReportModemStatus (object sender, EventArgs args) { }. 
  2. Events are instance fields of a class, so you need to have a variable pointing to an instance of the class with the event. Type var.eventname , where var is the variable pointing to the object that has the event and eventname is the name of the event. If the event is static, you can just type the name of the class followed by the name of the event.

  3. Type the plus equal operator += .

  4. Type new .

  5. Type the name of the delegate the event is based on.

  6. Type an open parenthesis ( .

  7. Type the name of the function in your class that you entered in step 1.

  8. Type a close parenthesis ) .

  9. Type a semicolon ; ( Figure 10.14 ).

    Figure 10.14 Capturing an event is a matter of adding a function to our class that has the same format as the event's delegate (in this case EventHandler). We then type the name of the event followed by += , followed by a new instance of the delegate with our function as the parameter for the delegate's constructor.
     public class WebForm1 : System.Web.UI.Page {  private void ReportModemStatus(   object sender, EventArgs args)   {  if (args is Modem.ReportStatusArgs)       {          Modem.ReportStatusArgs msg =          (Modem.ReportStatusArgs) args;          Response.Write(msg.Message                        + "<br>");       }    }    private void Page_Load(object sender,                 System.EventArgs e)    {  Modem m1 = new Modem();   m1.OnReportStatus += new   EventHandler(ReportModemStatus);  m1.Connect();    } } 

graphics/tick.gif Tips

  • When you use the += operator to subscribe to an event, the compiler adds code to invoke the add method of the class. Remember that when you add an event, the compiler turns the event declaration into a field and two functions, one to subscribe and one to unsubscribe.

  • To disconnect, or unsubscribe from an event, use the -= operator ( Figure 10.15 ).

    Figure 10.15 The Modem class keeps a list of listeners. If at some point we want to remove ourselves from the list, all we need to do is type the name of the event again, followed by -= , followed by a new instance of the event's delegate with our function as the parameter for the delegate's constructor.
     public class WebForm1 : System.Web.UI.Page {    private void ReportModemStatus(    object sender, EventArgs args)    {       if (args is Modem.ReportStatusArgs)       {          Modem.ReportStatusArgs msg =          (Modem.ReportStatusArgs) args;          Response.Write(msg.Message                        + "<br>");       }    }    private void Page_Load(object sender,                 System.EventArgs e)    {       Modem m1 = new Modem();       m1.OnReportStatus += new       EventHandler(ReportModemStatus);       m1.Connect();  m1.OnReportStatus -= new   EventHandler(ReportModemStatus);  } } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net