C# events and event handlers

What are Events in C#: In simple terms event means happening or action or occurrence of anything. Event can be anything it can be mouse click, enter key press, lost focus. In other words they are the notification, means something is going to change or ready to happen. Handling of the event is known as response. for example when you click on a button another page opens, here button click is an event and second part is the response of that events.  When events happens a notification sent to client so that client can prepare the response.

I am going to explain event handling in a very simple way, let’s suppose you have many objects in your application and they interact with other, now you tell me how you will know which of these many objects has changed their state? Any guess? Yes C sharp has the answer. You can do it by using
  • Event source
  • Event Handler
Event Source: It is responsible to inform other objects that something has changed.
Event Handler: When event source publish their signals about the changes, event handler receive those signals or events.  It can be static or non static method of your class. You can register multiple event handlers for one event.
In c share they are somehow related with delegates. Delegates plays a very important role in c sharp, they carries information from event source to event handler and taking back to the event source. It works between event source and event handlers. Events and Event handling are declared in a same class or another class with the help of delegates.
  •  How to use Events with Delegates?

WHAT IS EVENTS?

Events are nothing just a user action. For example
  1. When you click with mouse – It is mouse click events.
  2. When you press any key in keyboard – It is Key Press events
  3. When you refresh your webpage – It is page load events
  4. When you move mouse cursor – It is mouse hover events etc.
So when you take any action like key press, mouse movements, clicks etc an event raised. Let me clear more about it. For example, you filled an online form and click on submit button.
  1. In the background button_click() event raised.
  2. This event calls and execute an associated function Submit_Click().
  3. This function processes your request and submits page information to database.

HOW EVENTS WORK WITH DELEGATES?

Delegates are used to reference a method. An Event is associated with Event Handler using Delegates. When an Event raise, it sends a signal to delegates and delegates executes the right function.

WHAT IS PUBLISHER-SUBSCRIBER MODEL?

There are two parts in any event handling program. One part is Publisher that contains definition of events and delegates and another part is Subscriber that accepts the event and provides an event handler.

IMPORTANT FACT ABOUT EVENTS

  1. An Event is created using event keyword.
  2. An Event has no return type and it is always void.
  3. All events are based on delegates.
  4. All the published events must have a listening object.
  5. All Events should be defined starting with “On” keyword.
Let’s understand all these theory using Programming Example
Before seeing the programming examples you must know the sequential steps to manipulate events.
Step 1: Define a Delegate
Step 2: Define an Event with same name of Delegates.
Step 3: Define an Event Handler that respond when event raised.
Step 4: You must have method ready for delegates.

PROGRAMMING EXAMPLE:

This program simply adds two numbers. Only condition is if the sum of number is odd it fires an event that print a message using delegates.

EXPLANATION


Output:
********Event Executed : This is Odd Number**********
_

Comments

Popular posts from this blog

Visual Basic 6 (VB6) Operators

Control Structures In Visual Basic 6.0

C# Delegates simple example in .NET