SunQuest
 
       C#
  Home arrow C# arrow Page 5 - C# Events Explained
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Events Explained
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 41
    2007-02-13

    Table of Contents:
  • C# Events Explained
  • The Windows Form Example
  • The Steps of Creating an Event
  • The Bookshop Class and the AddBookEventHandler Delegate
  • The Notifier and the Sys Classes
  • Event Without an Event

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

    More C# Articles
    More By Michael Youssef


       · I work with C# for 9 months now and I couldn't understand events until I read your...
       · I always liked Michael intuitive writing style about various .NET topics and this...
       · Thanks for your comments about my article. Actually, each and every one of us who...
       · Mr.Michael I haven't read any of your articles and my first one was C# events. I'm...
       · I'm happy that you liked my articles and yes I plan to write about ASP.NET 2.0 very...
       · I work with different programming languages, and never meet such understanddable...
       · Glad you liked my articles. Could you please tell me what are those topics you want...
       · First, fantastic article. The best explanation of events that I have read to...
       · Hi,Thanks for your comment, I tried to do my best explaining those basics to...
       · Thanks Michael,That is what I was asking. I did not realise that there would...
       · You are welcome
       · Excellent article as well as the article on delegates which is a must read. Can you...
       · Thanks, I hope I can write more articles about C# soon. Anyway, I'm writing more...
       · You might also want to read my articles about C# Properties and Indexers. You will...
     

    C# ARTICLES

    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway