C#
  Home arrow C# arrow Page 3 - 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 
Mobile Linux 
App Generation ROI 
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 / 46
    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


    C# Events Explained - The Steps of Creating an Event


    (Page 3 of 6 )

    The very first step in creating an event is to define the signature of the event handler methods of the watcher objects in a delegate type. Why? Because events are based on delegates which encapsulate methods with a specific signature. As you know .NET Delegates are type-safe. Then you create a class that raises the event, the Publisher. In this class you define the event of the specific delegate type using the event keyword then define a method that fires the event. The last step is to create a watcher object that has an event handler method that will be called when the event takes place.

    Let's assume that we have a bookshop which fires an event each time a book is added to the bookshop. Another object needs to be notified each time a book is added to the bookshop to send the members an e-mail message to tell them about the new book. We need the following classes in order to create an event-based application:

    1. The Book class which represents a book.
    2. The BookShop class that fires the event.
    3. The AddBookEventHandler Delegate type that defines the signature of the event handler methods.
    4. The BookEventArgs class to carry the event data.
    5. The Notifier class which represents the watcher object.
    6. The Sys class that contains the Main() method which creates the objects and test the event.

    Let's create those classes step by step.

    The Book Class

    The Book class is very simple. It has three private fields with their public properties and one constructor.

    public class Book
    {
      private string title;
      private string isbn;
      private decimal price;

      public Book(string title, string isbn, decimal price)
      {
        this.title = title;
        this.isbn = isbn;
        this.price = price;
      }

      public string Title
      {
        get{return this.title;}
      }
      public string ISBN
      {
        get{return this.isbn;}
      }
      public decimal Price
      {
        get{return this.price;}
      }
    }

    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...
       · That was a good read...
       · one of the most clearly explained articles I have read this year. Thank you very...
       · Hi all,Thank you so much for your posts, .NET Windows Forms technology is based...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT