Friday, November 29, 2013

C# Event – Understanding C# events – Part 3

Before going further it is really important to go through the Part 1 and Part 2.
AS discussed in Part 2, I will create an application based on Events. So create BallEventArgs  class as shown below –
public class BallEventArgs : EventArgs
        public int Height { get; set; }
        public int Length { get; set; } 

        public BallEventArgs(int h, int l)
            Height = h;
            Length = l;

Create Ball class as hown below with event declaration –
public class Ball
        //declare event BallInGround
        public event EventHandler BallInGround; 

        /// <summary>
        /// method used for raising the event BallInGround
        /// </summary>
        /// <param name="e">BallEventArgs object</param>
        public void OnBallInGround(BallEventArgs e)
            if (BallInGround != null)
                BallInGround(this, e);

Add a Fielder class as shown below –

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 –  

Sunday, November 10, 2013

C# event – Understanding c# events – part 1

Event – a noun – described as “something that happens, especially one of importance”.
Example – The formula 1 race was an amazing event to see. 

C# events are no different from description stated above. The only difference being “Something happens on - Objects”. Any c# language feature can be well understood with real life example only. C# being OOP based language; let’s consider a real life example.
Let’s consider a popular game of Cricket to understand C# events.
Suppose you are developing a wonderful game of cricket for cricket lovers. Assume that this game is going to shake the world of gaming and you will be earning billions with the popularity of this game. At first to develop basic version you need Ball, Fielder, and Umpire and Fan (the person who is crazy about cricket) objects. Here are the responsibilities of every object –
Ball – Lets fielder know that I am in the field and catch me.

Fielder – fields or catches the ball when ball is in the ground.

Umpire – Watches the activity when Ball is in the ground and responds accordingly.

Fan – Watches the activities happening on the Ball and yells, cheers up, celebrates and many more!!!

Now everything is in place now you just need to connect everything together. So following diagram illustrates what we wish to do – 
However there is problem because Ball does not know which fielder out of 11 present on the ground will caught or grab him. Similarly Fielder only needs to know about fielding or catching a ball. Hence Ball should only get hit and should not know about the fielder; whereas fielder should only know about catching the ball.

In short – We want every object to know about it and not others. Hence we wish to separate the concerns of each object.

When the ball is hit we will use an EVENT to let fielders know about it. So those, our fielders can respond to the event of ball. Similarly we can make other objects like Umpire, Fan to know about Ball event so that they can also respond to it. So we want Ball object to raise an event. Then we will have other objects subscribed to that type of Ball event. When event is raised the subscribed objects will be notified and they can take the action the way they want. This action to be taken when an event is raised, written in Event Handler. The event handler code is executed every time the event is raised. It is just a method in the subscriber object which runs every time event is raised.