Monday, November 11, 2013

C# event – Understanding c# Event, Event Handler and event Arguments – part 2


In this post I will explain how you can declare an Event, event Handler and Event Arguments class in c#.
 
Event Arguments –
As per the example of Cricket application we have discussed in part 1 we need a class who will explain more information about the event BallInGround so that object like Fielder, Umpire and Fan can respond to it. This we will achieve by defining Event Arguments class. .NET framework provides a standard class for declaring event arguments and it is called as EventArgs. This class has no members. This class is used for passing the event arguments to event handler method. So out declaration will be as follows –

public class BallEventArgs : EventArgs 

Event –
Next we need to declare event in a class Ball. This can be declared with c# keyword “Event”. This keyword is used to inform other objects who are interested to subscribe to it. Event declaration is as follows –

public event EventHandler BallInGround; 

Events are usually public so that other interested objects can subscribe to them. In our case other objects will be Fielder, Umpire and Fan. In above event declaration we have used EventHandler keyword. For now understand it as a keyword which used to describe the event handler method signature for this event. This is defined in .NET. When you write EventHandler after an event declaration means you state that the event handler methods will have 2 parameters, an object named sender and an EventArgs class reference variable e and void return type. [EventHandler keyword here is actually a delegate but for now to keep focus on Event understand it as a keyword provided in .NET or understand it as a event of type EventHandler]. 

Event Handler –
Each object of Fielder, Umpire and Fan will subscribe to BallInGround event. Event handler methods you have been using for a long time. When you add a button on UI and double click on it a method get’s added in code behind which is nothing but an event handler method. So event handler method for BallInGround will also be similar to button click event handler method. You might have seen following type of declaration many times –  


void ball_BallInGround(object sender, EventArgs e) 

In above declaration the name of object reference, followed by underscore and then name of event is a standard naming convention followed in .NET to declare event handler methods. Hence the name - ball_BallInGround. 

Subscribing to an Event –
Once we have event handler method written in various objects like Fielder, Umpire, Fan all is remaining is just about registering for notification about event. This is also termed as subscribing to event or hook up to event. Say our reference variable is ball then event subscribing will be as follows –
 
ball.BallInGround += new EventHandler(ball_BallInGround); 

ball.BallInGround statement tells compiler to subscribe event handler to BallInground event of any object of type Ball.

+=  This sign is the way to subscriber to event. A part of syntax.

new EventHandler(ball_BallInGround); This statement specifies which event handler method to subscribe to the event.

ball_BallInGround – is the name of event handler method. 

Raising an Event –
Ball object will notify subscribers that it is in the ground. This is done by raising an event. Following statement explains how to rise an event –
    if (BallInGround != null)
            {
                BallInGround(this, e);
            } 

This explains all steps about using an event in c#. Next part will be to create a live Cricket applicationusing Events in Part 3 - Click here.
C# Events - Part 1.

No comments:

Post a Comment