473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Reasoning behind the EventHandler(object, EventArgs) signature

What is the reasoning for recommending that all events have the
signature EventHandler(object sender, EventArgs e).
Obviously I can create a custom delegate
delegate void FooHandler(string foo)
and event FooHandler FooHappened

What are the benefits of creating derived eventargs classes for each
event instead of creating the delegate which is much easier IMO.

Am I missing something?

Cheers
JB
Jan 30 '07 #1
6 2953
I guess for the same reasons that any good architect will impose some
sort of order on any framework they design.

The signature reassures the developer that a certain behaviour is
expected and that the sender of the event and some kind of event
argument, either raw or derived, will be provided. A void return type is
also imposing order and suggesting that info will be passed back in the
event arguments.

Creating your own delegates is perfectly acceptable but, if you were
writing any sort of commercial software, the choice would be nonsense
because it wouldn't fit with the plan.

Systems that use reflection to discover the capabilities of a component,
and there are lots of them these days, are also aware of the signature
so things like databinding use these events in the data change
notification system.

You would be strongly advised not to break what doesn't need fixing.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

John B wrote:
What is the reasoning for recommending that all events have the
signature EventHandler(object sender, EventArgs e).
Obviously I can create a custom delegate
delegate void FooHandler(string foo)
and event FooHandler FooHappened

What are the benefits of creating derived eventargs classes for each
event instead of creating the delegate which is much easier IMO.

Am I missing something?

Cheers
JB
Jan 30 '07 #2
On Jan 30, 4:00 am, John B <jbngs...@yahoo.comwrote:
What is the reasoning for recommending that all events have the
signature EventHandler(object sender, EventArgs e).
Obviously I can create a custom delegate
delegate void FooHandler(string foo)
and event FooHandler FooHappened

What are the benefits of creating derived eventargs classes for each
event instead of creating the delegate which is much easier IMO.

Am I missing something?
Well, prior to 2.0, you weren't. I often wondered the same thing -
it's not like you could use a less specific event handling delegate as
a more specific one. However, with 2.0 delegates can be covariant, so
it makes a lot more sense. I'm still not 100% convinced, but there's
more reason than there was before :)

Jon

Jan 30 '07 #3
This reasoning is from the .NET SLAR:

"
If we want to add some new information in the event callback we can do it
without breaking clients. For example, if we need to add information about
the location an assembly was loaded from we would simply add a new property
to the AssemblyLoadEventArgs class. If we did not have the EventArgs class
we would have to create a whole new Delegate and a new event of that type
with a different name. It would be really ugly. Encapsulating the event data
in a subclass of EventArgs makes this scenario very smooth.
"

I do agree, if you are making a class library or components for external
use. Having a common base class is good (see Exception) choice.
You can pass your derived (extended) class to relevant standard events if
needed, as they expect EventArgs derived class.
Another question are the standards, the common ways to do things. When all
events have EventArgs (and sender), something different can disturb.

"John B" <jb******@yahoo.comha scritto nel messaggio
news:9w*******************@nasal.pacific.net.au...
What is the reasoning for recommending that all events have the signature
EventHandler(object sender, EventArgs e).
Obviously I can create a custom delegate
delegate void FooHandler(string foo)
and event FooHandler FooHappened

What are the benefits of creating derived eventargs classes for each event
instead of creating the delegate which is much easier IMO.

Am I missing something?

Cheers
JB

Jan 30 '07 #4
Thanks for the responses guys.
Excellent information and viewpoints.

Cheers
JB
Jan 30 '07 #5
On Jan 30, 3:31 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
Well, prior to 2.0, you weren't. I often wondered the same thing -
it's not like you could use a less specific event handling delegate as
a more specific one. However, with 2.0 delegates can becovariant, so
it makes a lot more sense. I'm still not 100% convinced, but there's
more reason than there was before :)

Jon
Jon,

Did you mean contravariant? EventHandler has a void return type so I
don't think that would be in play. The parameters would though,
right?

Why doesn't C# support covariant return types on virtual methods in
subclasses? I thought I read somewhere that a newer version of Java
does.

Brian
Jan 31 '07 #6
Brian Gideon <br*********@yahoo.comwrote:
Did you mean contravariant? EventHandler has a void return type so I
don't think that would be in play. The parameters would though,
right?
Yup. I'm a fool :)
Why doesn't C# support covariant return types on virtual methods in
subclasses? I thought I read somewhere that a newer version of Java
does.
I can't remember, to be honest. I know it was one of the possible side-
effects of putting generics in. I agree, it would be very nice.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 31 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sandor Heese | last post by:
Question, When using BeginInvoke (on a From to marshal to the UI thread) with the EventHandler delegate I see some strange behaviour. The problem is that I call the UpdateTextBox method with a...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
1
by: | last post by:
Hi guys! I am a beginner with aspx (i have some background with .asp). And i am trying tu use a 'code-behind' page in order to execute all transactions in that page and leave as clean as possible...
4
by: Carlo Marchesoni | last post by:
I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes=".....";) Now I need to run some code behind code BEFORE this JavaScript...
3
by: moondaddy | last post by:
C# 2.0 I have declared some event arguments like this: public class ModeStateChangedEventArgs : EventArgs { Gen.DataState myState; public ModeStateChangedEventArgs() { }
1
by: vishnu | last post by:
Hi, I am working on asp.net project which I converted the code fron VB to C# and instead of RaiseEvent in VB code I used the following code. using System; using System.Data; using...
2
by: Peter Oliphant | last post by:
I've written a few event handler methods in my time. Most of them are of the general form: void EventHandler( Object^ o, EventArgs^ e ) { } However, in all of these I've written, and in...
2
by: markliam | last post by:
I have auto-generated some code for a button by double clicking it. By default, the code is created with a return type of void and assigned to a click event. Now, I want the function to return a...
14
by: GiJeet | last post by:
Hello, just wondering, in the event model windows uses, they use an argument wrapper class called EventArgs which you can subclass to encapsulate all the arguments you need to pass to any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.