(Notes from HFC#)
Steps in Event execution
1)We need an event for event arguments
eg-> public class BallEventArgs : EventArgs
=> EventArgs is an empty class.It has no public members.Its sole purpose is to allow
your event arguments to be passed to the event handlers that use it .
2)Need to define the event in the class that'll raise it .
eg-> public event EventHandler BallInPlay
=>after EVENT keyword comes EVENTHANDLER.It is needed to tell object subscribing to event that
their event handlers need to take 2 parameters,an object named SENDER and an EVENTARGS reference names e,
and have no return value(void).
3)The subscribing classes need EventHandler methods
eg-> void ball_BallInPlay(object sender,EventArgs e )
=>Standard Naming Convention for EventHandlers -> NameOfObjectReference,followed by UNDERSCORE ,followed by
NameOfEvent.
4)Each individual object subscribes to the event
eg-> ball.BallInPlay += new EventHandler(ball_BallInPlay)
=>The += EVENT tells C# subscribe an event handler to an event.
5)A Ball Object raises its event to notify subscribers that it's in play
ex- if (BallInPlay !=null) BallInPlay(this,e)
Note: If you raise an event with no handlers ,it'll throw an exception.
No comments:
Post a Comment