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.


Event Arguments – or Event Args –

When ball is hit and it is in the sky the fielder has to take judgement based on height and distance of the ball, its position, time required to reach to the point ball touching the ground. He has to take decision based on these parameters whether he will catch it or just field it. This decision or in other words handling of event BallInGround depends on Ball object’s height and distance. Here Ball’s height and distance is nothing but Event arguments which provide more information about the event that rose.

In Short - When event is raised, it is handled in event Handler method. However event handler method always needs information about events based on which handler method can take action. This is provided in Event Arguments.
 
 
Overall Summary and steps about c# events and event handlers-
- Objects subscribe to the event.
-   Some action triggers an event.
Event is raised.
-   Subscribers get notifications about raised event.
- Each object who has subscribed to the event handles in its own way.
 
Here we complete the basic understanding of the events. Now we need to understand how to declare Event, Event Handler and Event Arguments using c#. We will have a look at it in part – 2.
 

No comments:

Post a Comment