Tuesday, 15 May 2012

.net - C# events initialisation -



.net - C# events initialisation -

consider "application" object (thrower) throwing ball object (receiver). event (ballisthrowed) happens when ball thrown.

here 2 classes :

then entry point :

and methods pointed delegate when events fired :

this working well.

now want comment line :

because want ball not thrown.

the result null exception :

this normal because @ point ballisthrowed null

to solve this, initilise event :

but problem code never taking event when decomment "receiver.register(thrower)"

my questions : how can have 2 method eventmethod fired ?

bear me: setting event handler ballisthrowed, utilize ithrowtheball() trigger event, code:

ballisthrowed(this, eventargs.empty);

when seek phone call event handler, in case ballisthrowed, must exists or throw nullreferenceexception. so, in order work, must exists event handler ballisthrowed. does, until comment line:

//ballisthrowed += new eventhandler(method.eventmethod2);

so need verify if eventhandler exists before firing it:

if (ballisthrowed != null) ballisthrowed(this, eventargs.empty);

c# .net events

No comments:

Post a Comment