Team LiB
Previous Section Next Section

Events

Events allow one object to notify another object that something has occurred. You can implement events using the following steps:

  1. Define a delegate in the namespace scope.

                   delegate int MyDelegateEventHandler(MyEventArgs e);
    
  2. Define a class to hold arguments that need to be passed in the event.

                public MyEventArgs :EventArgs
                     {
                             public int MyInt;
                             public long MyLong;
                             public string MyString;
                     }
    
  3. Define a class that will publish events.

                public class MyEventPublisher
                {
                        public event MyDelegateEventHandler MyEvent;
                        public int DoSomething(MyEventArgs e)
                        {
                            //Fire events to subscribers
                              MyEvent(MyString);
                        }
                }
    
  4. Define a class that will subscribe and handle the events.

                class MyEventSubscriber
                {
                      static void Main(string[] args)
                      {
                              MyEventPublisher EventPublisher =
                                new MyEventPublisher();
                              MyEventArgs MyArgs = new MyEventArgs();
                              MyArgs.Mystring = new string("Hello World");
                              EventPublisher.MyEvent +=
                                new MyDelegateEventHandler (MyHandler);
                              EventPublisher.DoSomething(MyArgs);
                              }
                      static int MyHandler(object myObject, MyEventArgs e)
                      {
                              Console.WriteLine(e.MyString);
                      }
                }
    

Events Best Practices

The following are some guidelines for using events:

  • Name your events using a verb and use Pascal casing.

  • Use the term raised, rather than fire, to describe your events.

  • For an events declaration of a delegate, always return void and pass two parameters: an object called sender, followed by an EventArgs called e. This will help to maintain the strategy of all the built-in events.

  • Create an invoking method to raise events.

  • Remember that an event can be raised only from within the class that declared it. This affects base classes as well. The suggested strategy for doing this is to declare a protected virtual method that raises the event.

  • Name the method that fires the event OnEventName.

When to Use an Event

You should consider using events in the following cases:

  • Client code registers for an event prior to the event being fired.

  • You want to notify more than one client.

  • You want end users to easily add listeners.

The following example demonstrates implementing events.

Code Example: Events
Start example

using System;

namespace Client.Chapter_8___Delegates_Events_and_Namespaces
{
      //Creates a method pointer
      public delegate int MyDelegateEventHandler(MyEventArgs e);
      class MyEventSubscriber
      {
            static void Main(string[] args)
            {
                  MyEventPublisher EventPublisher = new MyEventPublisher();
                  MyEventArgs MyArgs = new MyEventArgs();

                  MyArgs.MyString = "Hello World";
                 //Subscribes to an event
                  EventPublisher.MyEvent += new MyDelegateEventHandler(MyHandler);
                  EventPublisher.DoSomething(MyArgs);
            }
            static int MyHandler(MyEventArgs e)
            {
                  Console.WriteLine(e.MyString);
                  return 0;
            }
      }
      public class MyEventArgs: EventArgs
      {
            public int MyInt;
            public long MyLong;
            public string MyString;
      }
      public class MyEventPublisher
      {
            public event MyDelegateEventHandler MyEvent;
            public int DoSomething(MyEventArgs e)
            {
                  //Raises an event
                  MyEvent(e);
                  return 0;
            }
      }
}
End example

Team LiB
Previous Section Next Section