There are plenty of books on the subject for those who want some deeper insight into events and .Net programming. All books I’ve come across tend to be very verbose. This will be a brief How-To that just presents the rudimentary steps needed to add a custom event to your class.

The necessary steps are:

  1. Create an EventArgs class or use an existing EventArgs.
  2. Create an EventHandler delegate or use an existing EventHandler.
  3. Declare the event.
  4. Create a virtual method that raises the event.
  5. Throw the event when needed.

class ThrowEventDemo
{
	// 1. Create your custom EventArgs class
	public class DemoEventArgs : EventArgs
	{
		private string message;

		public DemoEventArgs(string message)
		{
			this.message = message;
		}

		public string Message
		{
			get { return this.message; }
		}
	}

	// 2. Create your custom EventHandler delegate
	public delegate void DemoEventHandler(object sender, DemoEventArgs args);

	// 3. Declare the Event
	public event DemoEventHandler DemoEvent;

	// 4. Create a virtual method that throws the Event
	protected virtual void OnDemoEvent(DemoEventArgs e)
	{
		// Check if there are any subscribers before throwing the Event
		if (DemoEvent != null)
		{
			DemoEvent(this, e);
		}
	}

	// 5. A simple method that throws the Event
	public void DoSomething()
	{
		// Create an instance of DemoEventArgs
		DemoEventArgs e = new DemoEventArgs("My message!");
		// Throw the Event using our virtual method
		OnDemoEvent(e);
	}
}

In order to be able to catch the event you must subscribe to it and have a method that adhers to the apropriate EventHandler delegate to handle it.

class CatchEventDemo
{
	private ThrowEventDemo throwEventDemo;

	public CatchEventDemo()
	{
		// Subscribe to the event by adding an EventHandler submitting
		// the name of the method that is going to handle the event.
		throwEventDemo.DemoEvent += new ThrowEventDemo.DemoEventHandler(throwEventDemo_DemoEvent);
	}

	// This is the method that will handle the event.
	private void throwEventDemo_DemoEvent(object sender, DemoEventArgs e)
	{
		// Do something because of the occured event.
	}
}

A brief disgression about event naming. As you may have noticed I’ve used suffixes on all my event classes, delegates and so on. These are based on recommendations found in one of Microsofts many books about .Net development. These guidelines are as follows:

1. Add the suffix “EventArgs” to your EventArgs classes.
2. Add the suffix “EventHandler” for your EventHandler delegates.
3. Add the suffix “Event” to your Event.
4. Add the prefix “On” to the event name for your virtual method that throws the event

And always remember to use descriptive names!

Technorati Tags: , , ,

Edit: 2006-09-21 18:48

After some soul searching and looking at real world examples I realized that I do not follow naming guideline number 3. Calling an event StartedEvent sounds stupid so I usually name the event Started. Microsoft’s events have names like Click (Button_Click) so I should probably just name the event Start.

One Response to “How to write custom events in C#.Net”

Gravatar
Sweden

I just noted the generic EventHandler<T> in .Net Framework 2.0 which drastically reduces the amount of code needed to declare a custom event. Well drastically is a bit drastic. :-) It saves one line of code; there is no longer any need to declare a custom EventHandler delegate. In the previous example steps 2 and 3 can be replaced by the following:

public event EventHandler<DemoEventArgs> DemoEvent;