Geeks With Blogs
Dheeman Dutta Just Another Blog.....

I was surprised to see the syntax of the new Generic Eventhandler... Using the .net 1.1 way we can declare events using the following steps..

STEP 1: declare a delegate

public delegate void DivThreeHandler(object ,DivByThreeEventArgs e);

The abobe delegate declares the parameters to be sent to the event handlers.Any class that wants to handle this event needs to have a method that mathces this signature.

STEP 2: define the EventArgs derived class

public class DivByThreeEventArgs:EvetArgs
{
   ...........code.......
}

STEP3: define the class that want to be notified of the event.

public class DivByThreeListener
{
 public void Display(object o,DivByThreeEventArgs e)
  {
  ...............
  }
}

As you can see that the signature of Display mathces with the delegate.

STEP4: Declare the event and notify the intended class.

in event generating class ....declare the event
 
 public static event DivByThreeEventHndler EventThree;

 public static void Main()
     {
         DivByThreeListener dbl = new DivByThreeListener();
         EventThree += new DivByThreeHandler(dbl.Display);
         GenerateNumbers();
     }

 Now the function(in the same class) that invokes the event and thus notifies all  clients..........

 public static void OnEventThree(DivByThreeEventArgs e)
  {
       if(EventThree!=null)
          EventThree(new object(),e);
  }

 EventThree is null if no client is hooked to the delegate..

This was the C# 1.1 way . As C# 2.0 was introduced generics came in and so did Generic EventHandler.

=================================================================================================================

The foll. is taken from MSDN library :-

 EventHandler is a predefined delegate that represents an event handler method for an event, regardless of whether the event generates event data. If your event does not generate event data, substitute EventArgs for the generic type parameter; otherwise, supply your own custom event data type and substitute it for the generic type parameter.

The advantage of using EventHandler is that you do not need to code your own custom delegate if your event generates event data.

==================================================================================================================

namespace GenericEvnetHandler
{
    //---------------------------------------------------------
    public class MyEventArgs : EventArgs
    {
        private string msg;

        public MyEventArgs(string messageData)
        {
            msg = messageData;
        }
        public string Message
        {
            get { return msg; }
            set { msg = value; }
        }
    }
    //---------------------------------------------------------
    //---------------------------------------------------------
    public class ListenEvent
    {

        public static void SampleEventHandler(object src, MyEventArgs mea)
        {
            Console.WriteLine(mea.Message);
            Console.ReadKey();
        }

      
    }
    //---------------------------------------------------------
    class Program
    {
        // Declare an event of delegate type EventHandler of
        // MyEventArgs.


      


        public static event EventHandler<MyEventArgs> SampleEvent;
        public static void Main()
        {
            ListenEvent le = new ListenEvent();
            SampleEvent += new EventHandler<MyEventArgs>(ListenEvent.SampleEventHandler);
            OnDemoEvent("Hey there, Bruce!");
            OnDemoEvent("How are you today?");
            OnDemoEvent("I'm pretty good.");
            OnDemoEvent("Thanks for asking!");
        }

        public static void OnDemoEvent(string val)
        {
            // Copy to a temporary variable to be thread-safe.
            EventHandler<MyEventArgs> temp = SampleEvent;
            if (temp != null)
                temp(null , new MyEventArgs(val));
        }
       
    }
}

 

Cheers...

Posted on Thursday, February 15, 2007 7:17 AM | Back to top


Comments on this post: Generic EventHandler a new way of handling events

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Dheeman Dutta | Powered by: GeeksWithBlogs.net