SHASHANGKA SHEKHAR'S BLOG

Hi, I am Shashangka Shekhar, Working with Microsoft Technologies. This is my personal blog where I share some of my experiences.

Events

In this article we will try to understand what is event and how to handle and raise Events.

Note: Before start reading this article please hold a clear concept on delegates. Follow the link to read the previous article on Delegates.

What is Event?

  1. Event is a way to communicate in between of objects, which send notification to its subscribers (who register for that Event).
  2. Events are declared with an associated delegate.
  3. We know previously that delegate hold a reference to a method.
  4. Events sender send notification that’s how event raised and receiver receive the notifications.
  5. We can extends application by adding new methods without changing existing code.
  6. To build loosely coupled applications.
  7. The class containing the event is used to publish the event, called Publisher/Sender.
  8. Containing information’s about the event known as event arguments.
  9. The class accepts this event is called the Subscriber/Receiver.
  10. The method that execute by publisher call is known as event handlers

Diagram of Event Sender and Receiver:
event

Let’s demonstrate with a Sample Example:

OutPut:
Exam is Starting..
Your Time starts now | start answering..

Let’s consider the code step by step:
In this code the publisher is IQTest class, there is a method named OnExamStarted will notifies the attended exam students that’s the Exam is started.

Steps of an Event Sender:

  1. Define a Delegates
  2. Define an event based on that Delegates.
  3. Raise/Publish the event

Point 1: Define a Delegates
As we can see here to define a delegate is similar as method declaration as we seen in previous article on Delegates.

As we know delegate points a method. Here delegate respond between Publisher/Sender and Subscriber/Receiver. Establish the event handler method.

Point 2: Define an event
An event is declared based on the associated Delegates.

Button click, Text change of a textbox are known as event. Those are generally seen in windows form application.

Point 3: Publish the event
To publish an event we needs to check that event if null or not. This method is responsible for notifying the all subscriber.

Here we can see that there are two parameters passed in ExamStarted method, the first parameter (this) which is for the source of the event (current objects) and the second one is to send additional data with that event (this time it’s empty).

Register a Handler for that Event:
This is done with the += operators. We can also have multiple functions registering for the same event, all registered functions are called one by one.

If we want to remove any function, the -= operator is used.

Example of Sending Event Arguments:
Let’s modify the previous Example, this time we will add a new class called ExamEventArgs which has a property named examTime.

The class ExamEventArgs is inheriting from EventArgs.

And instead of Empty arguments

We are using a new instance of ExamEventArgs with initializing time to 10.

ExamStarted(this, new ExamEventArgs

Finally Merged sample Example:

OutPut:
Exam is Starting..
Your Time starts now | start answering..
Exam Start Time: 10

Types of Event Handler:

The Event Handler has two forms, they are:

  1. Normal Form. – EventHandler
  2. Generic Form. – EventHandler<T>

Our Previous example was with normal form of EventHandler.

Instead of creating our own we can use this Generic form. Let’s modify the previous Example instead of using our own custom delegate.

We can use a single line of code, and the output will be the same as previous.

Finally sample Example:

OutPut:
Exam is Starting..
Your Time starts now | start answering..
Exam Start Time: 10

Overview:

  1. Events are implemented with delegates.
  2. Events communicate in between of objects.
  3. A method that handles an event is called an event handler.
  4. To build loosely coupled applications events are helpful.
  5. We can extends application by adding new methods without changing other parts of the code.

One Comment

Post a comment