Events that conform to Net guidelines - I don't see the benefits

  • Thread starter Thread starter beginwithl
  • Start date Start date
B

beginwithl

hi


First I’d like to point out that I do understand how to use Events
according to Net Framework guidelines, I just don’t understand the
benefits of this Event pattern



From MSDN site:

“The .NET Framework guidelines indicate that the delegate type used
for an event should take two parameters, an "object source" parameter
indicating the source of the event, and an "e" parameter that
encapsulates any additional information about the event. The type of
the "e" parameter should derive from the EventArgs class. For events
that do not use any additional information, the .NET Framework has
already defined an appropriate delegate type: EventHandler.”

a) I don’t understand why it would be so much more useful for all the
delegate types used for events to have the same first parameter, and
having EventArgs or class derived from it as a second parameter?





b) Whatever the benefits may be, why would they out-weight the trouble
of having to
* write an additional code to put the desired arguments into an
object ( of type derived from EventArgs ) that will be passed as an
argument when event is fired
* write an additional code inside event handlers to extract
information from objects ( of types derived from EventArgs)
?


thank you
 
01: The "object sender" is useful because multiple instances can have events
set to the same method. For example if you have 10 buttons on a form the
Click event can all be set to Button1_Click() on your form, so it's a good
way of identifying the initiator of the event call.

02: The point of using EventArgs is to save you from updating lots of code
when an event signature changes. For example if you were to just pass the
arguments as parameters

public delegate void SomeEventHandler(object sender, string name, string
age);

You might have 100 places where you use SomeClass.SomeEvent, all having this
signature. Now what happens when you need to add telephoneNumber.

public delegate void SomeEventHandler(object sender, string name, string
age, string telephoneNumber);

Now you have to go to 100 places and add this additional parameter, whereas
with the EventArgs approach you would just add the additional property to
the EventArgs class and you don't have to update any of those 100 methods
(unless of course they need to access the new value).
 
hi
01: The "object sender" is useful because multiple instances can have events
set to the same method. For example if you have 10 buttons on a form the
Click event can all be set to Button1_Click() on your form, so it's a good
way of identifying the initiator of the event call.

a) So you¡¦re saying that an observer ( a class or an object that
contains an event handler ) may register the same method to several
publishers and thus it would be useful to know which publisher fired
an event, which in turn called observer¡¦s event handler?


b) But what if an event is declared as static and thus a publisher is
not an object but a class? What argument is then passed to ¡§object
sender¡¨ parameter?




02: The point of using EventArgs is to save you from updating lots of code
when an event signature changes. For example if you were to just pass the
arguments as parameters
public delegate void SomeEventHandler(object sender, string name, string
age);
You might have 100 places where you use SomeClass.SomeEvent, all having
this
signature. Now what happens when you need to add telephoneNumber.
public delegate void SomeEventHandler(object sender, string name, string
age, string telephoneNumber);

By ¡§You might have 100 places where you use SomeClass.SomeEvent, all
having this signature.¡¨ you mean that 100 event handlers might
subscribe to this event or that 100 publishers might use this event?




Now you have to go to 100 places and add this additional parameter, whereas
with the EventArgs approach you would just add the additional property to
the EventArgs class and you don't have to update any of those 100 methods
(unless of course they need to access the new value).


a) When you say that ¡§when we want to update an event by adding
another parameter, we can now add additional property to EventArgs
class¡¨ „³ do you mean we should add this new property to EventArgs
class itself or to a class derived from EventArgs?

b) Say we have class A, which is derived from EventArgs. If we wanted
to add another property, would it be better if we created a new class
B ( which we derive from A ), and add this new property to B, or
should we add new property to class A itself? In either case, the
already existing event handlers would still work correctly?

c) Are there any other reason why event handlers should have such
signatures?


in any case, thank you very much
 
Back
Top