Share via


Events using delegates (c#) – An additional example

A former colleague wrote an excellent blog post about using Events with Delegates, in C#. He is one of the smartest people I know and very active in the community, with a deep understanding of a dozen technologies with never ending passion for technology. I haven't found a simpler example on the web yet. I wanted to add an additional real world example written in ASP.net – The example is of a News paper (Publisher) and Customers (Subscribers). Here it is:

    1: /* Event delegation works on the Publisher <-> Subscriber Model. The Publisher exposes events that clients who are interested can subscribe
    2:  * to and when an event occurs, the subscribers are notified.
    3:  * If we were to consider a common real world example, we could think of implementing a Newspapaer and its Subscibers
    4:  * The code below implements the Newspaper as the Publisher and its Subscribers as customers who subscribe to be notified of breaking news.
    5:  */
    6:  
    7: using System;
    8: using System.Web;
    9:  
   10: namespace Example4
   11: {
   12:     public partial class EventsDemo : System.Web.UI.Page
   13:     { 
   14:         protected void Page_Load(object sender, EventArgs e)
   15:         {
   16:             //We will first instantiate our Publisher - Which is our news paper - TimesOfIndia
   17:             TimesOfIndia TOI = new TimesOfIndia();
   18:  
   19:             //Add subscribers
   20:             TOICustomer Sudeepg = new TOICustomer(TOI, "Sudeepg");
   21:             TOICustomer Jack    = new TOICustomer(TOI, "Jack");
   22:             TOICustomer George  = new TOICustomer(TOI, "George");
   23:  
   24:             //Times now will get the breaking news for us
   25:             TOI.getBreakingNews();
   26:         }
   27:     }
   28:  
   29:     //This is our public delegate that will be hooked up by our subscribers.
   30:     public delegate void BreakingNewsEventHandler(object sender, BreakingNewsEventArgs e);
   31:  
   32:     public class TimesOfIndia  // Times of india news paper is our publisher.
   33:     {
   34:         public event BreakingNewsEventHandler breakingNewsNotifyList;
   35:  
   36:         public void getBreakingNews()
   37:         {
   38:             string BreakingNews = "Annual Sale - Best deals on electronic devices!";   //Sample breaking news
   39:  
   40:             //Display the breaking news - NOTE: The Marquee tag only works in IE. Remove it for other browsers.
   41:             HttpContext.Current.Response.Write("<h1><marquee> Current Breaking News : " + BreakingNews.ToString() + "</marquee></h1><hr />");
   42:  
   43:             //Create and add event args
   44:             BreakingNewsEventArgs e = new BreakingNewsEventArgs(BreakingNews);
   45:  
   46:             //We need to invoke the notification, ONLY if there are subscribers to breaking news notification. That's why we check for NULL.
   47:             if (breakingNewsNotifyList != null)
   48:                 breakingNewsNotifyList(this, e);
   49:  
   50:         }
   51:     }
   52:  
   53:     public class BreakingNewsEventArgs : EventArgs      //Class used to pass data to event consumers when sending notification.
   54:     {
   55:         public BreakingNewsEventArgs(string BreakingNews)
   56:         {
   57:             this.m_BreakingNews = BreakingNews;         //Save the breaking news into a property.
   58:         }
   59:  
   60:         string m_BreakingNews = "";
   61:         public string BreakingNews      //Data we are passing to consumers is the breaking news itself (Stored as property).
   62:         {
   63:             get { return m_BreakingNews; }
   64:         }
   65:     }
   66:  
   67:  
   68:     public class TOICustomer        //Instances of this class will be the subscribers of Times of India (Publisher).
   69:     {
   70:         private TimesOfIndia TOI;               //Copy of the newsPaper
   71:         private string m_CustomerName = "";     //Store the customer name
   72:  
   73:         public TOICustomer(TimesOfIndia TOICopy, string customerName)
   74:         {
   75:             this.TOI = TOICopy;
   76:             m_CustomerName = customerName;
   77:             TOI.breakingNewsNotifyList += new BreakingNewsEventHandler(OnRecvBreakingNews);  //Subscribe to breaking news notification
   78:         }
   79:  
   80:         //This method will be invoked upon a breaking news Notification.
   81:         private void OnRecvBreakingNews(object sender, BreakingNewsEventArgs e)
   82:         {
   83:             HttpContext.Current.Response.Write(m_CustomerName + " Acknowledging breaking news : " + e.BreakingNews.ToString() + "<br />");
   84:         }
   85:     }
   86: }

Comments

  • Anonymous
    January 21, 2010
    Lot of thanks for sharing your colleague's post. Really it is very for me