Declaring and Firing Events


Events are essential entities for Web programming. They are functions that are used to report user interaction with Web forms and Web controls, among other objects. Events are built on top of delegates, so to declare an event in C# you first declare a delegate. The event declaration then tells the compiler to add a field of the delegate type and two functions: add and remove. The delegate field maintains a list of delegates by combining and removing delegates from the field. The add and remove functions enable classes to subscribe and unsubscribe from the list of delegates. By firing the event, the class invokes a method on all of the subscribers in the list.

To declare an event:

  1. Declare a delegate using the instructions in the section "Declaring a Delegate," above.

  2. Inside the class that is to issue event notifications, type an access modifier such as public, protected, private, etc.

  3. Type event .

  4. Type the name of the delegate type.

  5. Type a name for the event. Event names are usually constructed by adding the prefix On to the name of the delegate type. For example, if the delegate type is ReportStatus, the event name would be OnReportStatus.

  6. Type a semicolon ; .

To fire the event:

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

  2. Type an open parenthesis ( .

  3. Type the parameters for the delegate type.

  4. Type a close parenthesis ) .

  5. Type a semicolon ; ( Figure 10.11 shows the Modem class from the previous section modified to use events instead of the delegate variable directly).

    Figure 10.11 Notice the transformation between the last figure and this one. What we have done is to turn the delegate into an event. This makes the compiler give us the added ability to subscribe and unsubscribe from status notifications.
     delegate void  ReportStatusDel  (              string status); class Modem {    public static  event   ReportStatusDel OnReportStatus;  public static void Connect()    {       if (OnReportStatus != null)       {  OnReportStatus  (                    "Initializing...");  OnReportStatus  (                    "Dialing...");  OnReportStatus  (                    "Authenticating...");  OnReportStatus  ("Connected...");       }    } } 

graphics/tick.gif Tips

  • Before firing an event you have to check if the event variable is not equal to null.

  • Events are used to report user interactions with various objects. For example, the Page class has a number of events. Events in Web applications execute on the server. When a client accesses a page through the browser and interacts with a control, the client HTML issues a post command. The post command is an HTTP command that executes on the server. The server then creates various objects, and those objects generate events. For example, a button control may generate a click event; you then write code to handle the event.

  • Firing the event is simply invoking a delegate. Remember, a delegate is a type that stores a reference to a function in another class. Therefore the subscriber of the event has to have a function that the delegate will invoke. For instructions on how to subscribe and unsubscribe to events, see "Subscribing to Events," later in this chapter.




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