Wednesday, December 4, 2013

C# Events – Concepts and Caveats – Part 4

I hope you have gone through the Part 1 to 3 of C# events.
C# Events – NullReferenceException –
If you raise an event without handlers then you get this exception – Object reference not set to instance of an object. Look below screenshot. I have commented the registration of event from Fielder class and similarly for Fan class. Also commented null check from Ball class from event raising code –

 


 
The exception is reported because none of the objects have added their event handlers to an event BallInGround, hence it will be null. Therefore it is always necessary to check null condition before raising an event.
Use Standard Naming Conventions for Raising C# Events –
For example, Forms OnDoubleClick method raises Double Click evevnt. Similarly OnBallInGround raises BallInGround event.
Use of EventHandler in declaration of event –
When you subscribe to an event, you write event handler method for it. The keyword EventHandler here in the declaration specifies the signature of event and it tells the objects subscribing to it how they need to declare their event handlers. In our case, EventHandler tells Fielder and Fan object to declare event handler method which takes Object and EventArgs parameter and returns void. So EventHandler is standard event handler. If you take the cursor on EventHandler and press F12 then you will see the definition as follows –

So EventHandler is a standard event handler. Hence there are other kinds of event handlers as well. The parameters of event handler are defined by something called as delegate. The keyword “delegate” I will explain in next part of the series.
Can I have an event handler which returns something other than Void?
Technically it is possible and you can have. But it is not standard practice and not recommended. If you take event which return something other than void the chaining will not be possible. Means you can not subscribe or register multiple event handlers with same event as every handler will be returning different values. The bottom line of event usage is – Single event can be raised only by sigle object however can be responded by multiple objects. This will not be possible if event is returning other than void. And this is the reson why we add or subscribe or register and event handler to an event with sign +=. Means we add new event handler to existing handlers.
Creating event handlers automatically –
Following screenshot depicts how you can add event handler automatically on pressing Tab –

 

As shown above, the handler method always filled with NotImplementedException so while running it will throw a crash to let you know that, you need to write your logic there. Visual studio uses same idea to write event handlers automatically for you for the controls added like Button. The resgistration of event code is added automatically in Initialize method as shown below. The event is in Button class – 
 

 
Hope above concepts has given you better understading of C# events. Let’s discuss about Delegates in
Part – 5(In Progress).

No comments:

Post a Comment