Adding Events that are Web Friendly


A lot of Web- related classes fire events, and some fire many events. It would be really hard to keep track of the parameters of each event if the events didn't follow certain guidelines for their parameters. To ensure that most events follow the same parameter guidelines, Microsoft defined a delegate that developers should use when declaring their events. This delegate is called EventHandler . So when you declare an event, you should use EventHandler as the type.

To declare an event that has a standard format:

  1. Inside the class that will fire the event, type an access modifier, such as public.

  2. Type event .

  3. Type EventHandler .

  4. Type the name of the event. For example: OnReportStatus .

  5. Type a semicolon ; ( Figure 10.12 shows the Modem class from the previous section modified to use the standard event type, EventHandler, rather than its custom type, ReportStatusDel).

    Figure 10.12 Most events in C# follow the same format because they are all based on the EventHandler delegate. This delegate has two parameters, sender and arguments. However, right now we have no way of passing arguments, so all we can do is pass Empty as our argument. To pass arguments we need to create a special class, as in Figure 10.13.
     class Modem {    public event  EventHandler  OnReportStatus;    public void Connect()    {       if (OnReportStatus != null)       {          // "Initializing..."  OnReportStatus(this,   EventArgs.Empty);  // "Dialing..."          OnReportStatus(this,          EventArgs.Empty);          // "Authenticating..."          OnReportStatus(this,          EventArgs.Empty);          //"Connected..."          OnReportStatus(this,          EventArgs.Empty);       }    } } 

graphics/tick.gif Tips

  • EventHandler is a delegate derived from MulticastDelegate . The delegate has two parameters. The first parameter is sender of type object . The second parameter is args of type EventArgs .

  • When you fire an event of type EventHandler , the first parameter, sender , should be the class that is generating the event. Most likely this parameter would be set to this . The second parameter, args , is for sending information related to the event. If your event has related information, you would create a class derived from System.EventArgs and add a field to the class that represents the extra information. Then you would create an instance of the class and pass the instance of the class in the second parameter. In the example code in Figure 10.12 , the event is useless for our purpose because we need to also pass in the message for the Modem status. Figure 10.13 shows the code modified to pass the message information as well.

    Figure 10.13 The ReportStatusArgs function enables us to report extra information as part of our event. All we have to do to make our event behave like other events is derive our ReportStatusArgs class from EventArgs. We then store the extra information in an instance of this class and send it as the second parameter of our event call.
     class Modem {    public event EventHandler    OnReportStatus;  public class ReportStatusArgs :   EventArgs   {   public string Message;   }  public void Connect()    {       if (OnReportStatus != null)       {  ReportStatusArgs msg =   new ReportStatusArgs();   msg.Message = "Initializing...";   OnReportStatus(this,msg);   msg.Message = "Dialing...";   OnReportStatus(this,msg);   msg.Message="Authenticating...";   OnReportStatus(this,msg);   msg.Message = "Connected...";   OnReportStatus(this,msg);  }    } } 



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