Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple and Best Example on How to Work with Event Handlers

0.00/5 (No votes)
29 Jun 2012 1  
Simple and best example of how to work with Event Handlers.

Introduction

This article tries to explain the concept of Event Handling in C#.  This article uses a small scenario wherein a Wholesaler(Publisher) keeps changing the price of his article. There are Retailers(Subscriber)who want to register themselves for this price so that they can do the business... This is just like a stock broking or a bullion market...

Whenever the Publisher changes the Price it automatically changes the price at the Subscriber....

Initially create a class named Wholesaler which creates an event and an event and an EventHandler.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpPractice_Latest
{
    public class Wholesaler
    {
        private static int _price;
        
        //Declaring a delegate that handles the event.....
        public delegate void PriceChangeHandler(Wholesaler sender, PriceChangeEventArgs PCE);
        
        //Declaring an event....
        public event PriceChangeHandler PriceChange;
        
        public int Price
        {
            get
            {
                return Wholesaler._price;
            }
            set
            {
                Wholesaler._price = value;
                //Firing the event whenever the price is changed.
                OnPriceChange(this, new PriceChangeEventArgs(value));
            }
        }
        //This is the key method that propagates the event
        public void OnPriceChange(Wholesaler sender, PriceChangeEventArgs PCE)
        {
            PriceChange(this, PCE);
        }
    }
}

Creating a class that extends the EventArgs class of the .NET Framework, to carry the event data.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpPractice_Latest
{
    public class PriceChangeEventArgs : EventArgs
    {
        public int NewPrice { get; set; }

        public PriceChangeEventArgs(int NewPrice)
        {
            this.NewPrice = NewPrice;
        }
    }

Create a class Retailer (Subscriber) that is interested in the event fired by the wholesaler (Publisher). You can create any number of subscribers such as Retailer1, Retailer2, and so on to subsribe to the event fired by the Publisher.....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpPractice_Latest
{
    public class Retailer1
    {
        private int _price;

        public int Price
        {
            get { return _price; }
            set { _price = value; }
        }

        public void subscribe(Wholesaler P)
        {
            P.PriceChange += new Wholesaler.PriceChangeHandler(UpdatePrice);
        }

        public void unsubscribe(Wholesaler P)
        {
            P.PriceChange -= new Wholesaler.PriceChangeHandler(UpdatePrice);
        }

        private void UpdatePrice(Wholesaler P, PriceChangeEventArgs PCE)
        {
            this.Price = PCE.NewPrice;
        }
    }
}

Finally, the execution path.... How this works!!!!!!!!!!!!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpPractice_Latest
{
    class Program
    {

        static void Main(string[] args)
        {

            Wholesaler P = new Wholesaler();
            Retailer1 R1 = new Retailer1();
            Retailer2 R2 = new Retailer2();
            R1.subscribe(P);
            R2.subscribe(P);

            Console.Out.WriteLine("Initially the price with Wholesaler is Rs.{0}", P.Price);
            Console.Out.WriteLine("Initially the price with Retailer1 is Rs.{0}", R1.Price);
            Console.Out.WriteLine("Initially the price with Retailer2 is Rs.{0}", R2.Price);

            Console.Out.WriteLine("Now, changing the price with Wholesaler to Rs.100");

            P.Price = 100;

            Console.Out.WriteLine("Now the price with Wholesaler is Rs.{0}", P.Price);
            Console.Out.WriteLine("Now the price with Retailer1 is Rs.{0}", R1.Price);
            Console.Out.WriteLine("Now the price with Retailer2 is Rs.{0}", R2.Price);

            Console.Out.WriteLine("Retailer1 opted to withdraw the link with Wholesaler");
            R1.unsubscribe(P);
            Console.Out.WriteLine("Now, changing the price with Wholesaler to Rs.200");

            P.Price = 200;

            Console.Out.WriteLine("Now the price with Wholesaler is Rs.{0}", P.Price);
            Console.Out.WriteLine("Now the price with Retailer1 is Rs.{0}", R1.Price);
            Console.Out.WriteLine("Now the price with Retailer2 is Rs.{0}", R2.Price);


            Console.ReadLine();
        }
    }
}

The output is: 

Initially the price with Wholesaler is Rs.0
Initially the price with Retailer1 is Rs.0
Initially the price with Retailer2 is Rs.0
Now, changing the price with Wholesaler to Rs.100
Now the price with Wholesaler is Rs.100
Now the price with Retailer1 is Rs.100
Now the price with Retailer2 is Rs.100
Retailer1 opted to withdraw the link with Wholesaler
Now, changing the price with Wholesaler to Rs.200
Now the price with Wholesaler is Rs.200
Now the price with Retailer1 is Rs.100
Now the price with Retailer2 is Rs.200

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here