, , , , ,

I was talking to a friend over the weekend and the “Events in .NET” subject came up. Basically, over the years working with .NET, he had created several applications that responded to .NET events, but he had never worked on the other side of the fence: raising the event itself. During the conversation, I’ve realized there’s a possibility that creating classes that raise events is not a common trait among most developers as .NET Framework is extensive enough to address most common issues. So, I’ve decided to create this post to address how to raise and respond to events in .NET.

Let’s start with raising the event on the class side. There are three main steps you need to take when raising an event for a specific situation:

  1. Define a delegate to hold the reference to the method that will respond to the event. The responding method will be specified by the application instantiating the class, not the class itself.
  2. Define an event member associated with your delegate to ensure your delegate will be executed when the event occurs.
  3. Raise the event when the specific situation occurs.

To make things clear, let’s use a simple but real-life example. Let’s suppose you have a Customer class that holds customer records. As using your class is the only way applications can remove customer records from the system, you have been asked to find a way to notify the applications using your class anytime a user tries to delete a customer record, allowing them to validate and log the request before it actually takes place. You can also give applications a way to abort the process if the request is not authorized.

Following the scenario above, let’s apply the three main steps to raise an event.

public class Customer
// Step 1. Define a delegate
public delegate void DeletingCustomerEventHandler(object sender, EventArgs e);

// Step 2. Define an event member associated with your delegate
public event DeletingCustomerEventHandler DeletingCustomer;

// Method that raises the event
public bool DeleteCustomer()
//Step 3. Raise the event (occurs before the action takes place)
EventArgs e = new EventArgs();
if(this.DeletingCustomer != null)
this.DeletingCustomer(this, e);

// Continue deleting customer …

Breaking down the code: In step 1 above, the first parameter of DeletingCustomerEventHandler will be the object raising the event, and for the second parameter, you can either use the default EventArgs class or create your own class based on EventArgs. You’ll usually create your own class if you need to pass any extra information to the method responding to the event. Step 2 defines the DeletingCustomer event, which will be automatically exposed to application developers using your class. And step 3 basically verifies whether or not a method has been assigned to your delegate and raises the event if the delegate is not null.

Now that we have covered how to raise an event, let’s complete the post by talking about the part most developers are familiar with: responding to the event.

In order to respond to an event, application developers must follow two main steps:

  1. Create a method to respond to the raised event; the method must match the delegate signature for the event.
  2. Add an event handler associating your method created in step 1 to the event being raised; this step basically does the magic and allows the event to be raised (see step 3 in the previous section).

Let’s dive into the sample code to respond to the DeletingCustomer event of the Customer class:

// Step 1. Create a method to respond to the event
private void ValidateDeleteRequest(object sender, EventArgs e)
// If necessary, cast object back to Customer type
Customer oCust = (Customer)(sender);

// Log Request
LogRequest(oCustomer, oCurrentUser);

// Validate Request
if (!FunctionToValidateDeletion(oCust))
{ // Cancel Operation if allowed by the class }

// Step 2. Add event handler when creating the object
Customer oCustomer = new Customer();
oCustomer.DeletingCustomer += new Customer.DeletingCustomerEventHandler(ValidateDeleteRequest);

This code to respond to the event is simple… in step 1 we create a method matching the delegate signature for the event handler. This method will be called anytime a customer record is about to be deleted from the system. So, within the method, application developers can validate and log details of the request for later use. If needed, you can also allow the application to cancel the process when the validation fails by creating a new class based on EventArgs class, in which you would expose a member to trigger the cancellation process.

And in step 2, we ensure that the event will be actually raised by adding the event handler and associating our method with it. If you remember step 3 from the raising an event section above, before raising the event, we must check whether or not a method has been associated with our delegate. The step 2 in this section takes care of this association.

That’s all for now. I hope I have been able to clarify a little bit about how you can raise and respond to events in .NET. As we can see, there’s no mystery in the process; it’s just a matter of understanding how each part fits together to accomplish the goal.

Happy coding!