20
Answers

NP23 EventHandler

Maha

Maha

16y
4.9k
1

July 5, 2007

  

Hi Guys

 

I got the following program from a website. Address is given below. The program uses the built-in EventHandler delegate. I wish to know in what circumstance programmer has to decide whether he has to use custom made EventHandler delegate or built-in EventHandler delegate. Please help.   

 

Thank you

 

//Use the built-in EventHandler delegate

//http://www.java2s.com/Code/CSharp/Language-Basics/UsethebultinEventHandlerdelegate.htm

 

using System;

 

// Declare an event class.

class MyEvent

{

      public event EventHandler SomeEvent; // uses EventHandler delegate

 

      // This is called to fire SomeEvent.

      public void OnSomeEvent()

      {

            if(SomeEvent != null)

                  SomeEvent(this, EventArgs.Empty);

      }

}

 

public class EventDemo6

{

      static void handler(object source, EventArgs arg)

      {

            Console.WriteLine("Event occurred");

            Console.WriteLine("Source is " + source);

      }

 

      public static void Main()

      { 

            MyEvent evt = new MyEvent();

 

            // Add handler() to the event list.

            evt.SomeEvent += new EventHandler(handler);

 

            // Fire the event.

            evt.OnSomeEvent();

      }

}

Answers (20)