C# Events Explained - The Bookshop Class and the AddBookEventHandler Delegate
(Page 4 of 6 )
Let's first create the delegate:
public delegate void AddBookEventHandler(object source, BookEventArgs e);
The delegate encapsulates any method that takes two parameters, an object and a BookEventArgs instance, and returns void. Note that the BookEventArgs class, which is implemented in the next section, derives the EventArgs class. When you need to carry data for the event you must create a class that derives the EventArgs class. Next, let's look at the implementation of the BookShop class.
public class BookShop
{
private ArrayList books;
private string name;
public event AddBookEventHandler AddBook;
public BookShop(string name)
{
books = new ArrayList();
this.name = name;
}
protected void OnAddBook(BookEventArgs e)
{
if(AddBook != null)
AddBook(this, e);
}
public void AddNewBook(Book book)
{
this.books.Add(book);
BookEventArgs e = new BookEventArgs(book);
OnAddBook(e);
}
}
The Bookshop class contains a private ArrayList to store the books, but the interesting code begins with the declaration of the event. We have declared an event called AddBook of type AddBookEventHandler delegate with the event keyword. Why do we need to do this?
When you define an event as of a specific delegate type you standardize the signature of the methods that can be notified (through delegates) of the occurrence of that particular event.
Also the event creates a multicast delegate (of the delegate type it has been declared with). To maintain the assigned delegates of the watcher objects, you assign a delegate instance to a multicast delegate through the += operator.
The method OnAddBook() raises the AddBook event. It simply checks if the multicast delegate of the event is not null then calls the delegate using the event identifier. The method AddNewBook(), which calls the OnAddBook() method, accepts a Book instance as a parameter, then it adds the instance to the private ArrayList object. The method creates a new instance of the class BookEventArgs and passes the Book instance to the constructor. Then it calls the OnAddBook() method and passes the BookEventArgs instance to it.
As we said, the OnAddBook() method raises the event by invoking the multicast delegate. The signature of the delegate takes two parameters: the first is of type object which represents the object that raised the event and that's why we passed the this keyword and the second is the BookEventArgs instance, which is created by AddNewBook() then passed to OnAddBook() method, which passes it to the multicast delegate:
AddBook(this, e);
Client code should call the AddNewBook() method which in turn calls the method OnAddBook() that raises the event.
The BookEventArgs Class
The BookEventArgs class derives the EventArgs class. It contains data (the Book object newly added to the bookshop) for the event. Imagine that you subscribed to the AddBook event but you don't have access to the newly created Book instance inside the event handler method. I don't want to imagine something like that anyway. Here's the code of the class:
public class BookEventArgs: EventArgs
{
private Book book;
public BookEventArgs(Book book)
{
this.book = book;
}
public Book Book
{
get { return this.book;}
}
}
This class is passed the Book instance when it's created in the BookShop.AddNewBook() method so there's nothing special here. It takes the Book instance and stores it in a private field. You can read the object through the public property.
Next: The Notifier and the Sys Classes >>
More C# Articles
More By Michael Youssef
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|