C# Events Explained - The Notifier and the Sys Classes
(Page 5 of 6 )
The Notifier class has only one method that can be hooked up to the event through the delegate that will be created in the Sys Class's Main() method. The Method is called instance_AddBook(), but you can call it any name you like, and it has the same signature as the delegate in our example. Note that the method has access to the newly added book through the BookEventArgs parameter; thus we have created an event with data that is passed to the event handler method (the instance_AddBook() method). The method prints a message to the console to notify us that a message has been sent to the members about the book. Here's the code of the Notifier class:
public class Notifier
{
public void instance_AddBook(object source, BookEventArgs e)
{
Console.WriteLine("A Message has been sent to the Membersn" + " regarding the book '{0}' with the ISBN '{1}'n", e.Book.Title, e.Book.ISBN);
}
}
The Sys Class is the application that uses all the above classes. Let's look at the code:
public class Sys
{
public static void Main()
{
BookShop bookShop = new BookShop(".NET Bookshop");
Book b1 = new Book("The Right Way","123456789", 19.90m);
Book b2 = new Book("The .NET Stuff","987654321", 29.90m);
Notifier notify = new Notifier();
bookShop.AddBook += new
AddBookEventHandler(notify.instance_AddBook);
bookShop.AddNewBook(b1);
bookShop.AddNewBook(b2);
Console.ReadLine();
}
}
The Main() method creates an instance of BookShop, then creates two Books, a Notifier instance then hooks up the event handler method, notify.instance_AddBook() through a delegate type AddBookEventHandler instance, to the event AddBook through the += operator. Note that you can't use the assignment operator = because the event maintains a multicast delegate and if C# lets you assign a delegate using the = operator you would replace the multicast delegate with the new value which is just a delegate (singlecast delegate).
The bookShop.AddNewBook() takes a Book instance to add it and then it fires the event as we discussed. You need to put all the above classes into a namespace -- I called it Events -- then reference the System.Collections namespace because we have used an ArrayList object to store Book instances. Copy the classes to the VS.NET Class file and run it.

As you can see, the event handler method is called twice because the event has been fired twice. Also note that the use of the class BookEventArgs gives us the ability to tell the members about the Title and ISBN of the book through the properties of the Book instance of the BookEventArgs. Using the EventArgs class would give us the ability to tell them that a book has been added, but we can't tell them any information about the book because we don't have access to that object. I hope that by now you understand the event model and how it's useful for our applications.
By convention, the delegate identifier ends with EventHandler and the event identifier is up to you, but it has to describe an action that can happen, like a mouse click or a form load and so on. The name of the class that contains the event data ends with EventArgs and it must derive the System.EventArgs base class. The name of the method that raises the event must begin with On like our OnAddBook() method and like many other methods you are going to work with in Windows.Forms.
MSIL Generated Code
Load the application with ILDASM, navigate to the BookShop class and expand it.

Even though we have created the event AddBook as public it's private in the MSIL code as highlighted in the above screen shot. Note that the private field is of type Events.AddBookEventHandler which means that this field is using a delegate of type AddBookEventHandler to store the delegates that are called when the event takes place. The method Add_AddBook() is generated to add a delegate instance to the Multicast AddBookEventHandler delegate of the event, of course using the Delegate.Combine() method. The method remove_AddBook() is generated to remove a delegate from the event through the Delegate.Remove() method. The event itself is the member AddBook; here's the MSIL generated code:
.event Events.AddBookEventHandler AddBook
{
.addon instance void Events.BookShop::add_AddBook(class Events.AddBookEventHandler)
.removeon instance void Events.BookShop::remove_AddBook(class Events.AddBookEventHandler)
} // end of event BookShop::AddBook
As you can see, it's very similar to a property (get and set methods) but here, you see add and remove behavior. So you may say that we can do the same event model with delegates only without the use of events. Let's do exactly that.
Next: Event Without an Event >>
More C# Articles
More By Michael Youssef
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|