473,725 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why Pass EventArgs In Delegate When not needed?

All the event handlers seem to pass an Object and an EventArgs object.
If the event doesn't need this info why pass them anyway? It is inefficient.
Nov 16 '05 #1
8 4543
All the event handlers seem to pass an Object and an EventArgs object.
If the event doesn't need this info why pass them anyway? It is inefficient.


It's consistent with other event signatures and the .NET design
guidelines.

http://msdn.microsoft.com/library/en...Guidelines.asp
http://msdn.microsoft.com/library/en...Guidelines.asp

It's not like you're wasting significant clock cycles by passing an
extra parameter, especially if you just pass in EventArgs.Empty .

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Mattias Sjögren <ma************ ********@mvps.o rg> wrote:
All the event handlers seem to pass an Object and an EventArgs object.
If the event doesn't need this info why pass them anyway? It is inefficient.


It's consistent with other event signatures and the .NET design
guidelines.

http://msdn.microsoft.com/library/en...pconEventNamin
gGuidelines.asp
http://msdn.microsoft.com/library/en...pconEventUsage
Guidelines.asp

It's not like you're wasting significant clock cycles by passing an
extra parameter, especially if you just pass in EventArgs.Empty .


Unfortunately I think the convention is somewhat broken. For anything
that really uses a subclass of EventArgs (and let's face it, EventArgs
itself is pretty useless on its own) you want to use a more strongly
typed event handler - such as KeyPressEventHa ndler. Given that, why
specify EventArgs when there's nothing useful?

I must admit this is one part of the convention that I sometimes break
- I prefer to use

// No more information
void FooEventHandler (object sender);
// Strongly typed information
void BarEventHandler (object sender, BarEventArgs e);

I don't see the point of including a needless parameter - and the fact
that it's there sort of implies that there might be something useful
about it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Hi,

I also agree with Jon, EventArgs is useless. and I also break it all the
time.

One particular escenario when I'd see this happen is when a worker thread
need to signal the UI thread that it's done. All it needs is a method that
is executed in the main thread, it does not matter the sender, and even less
an arg.
Fortunely I think that the anonymous methods in C# 2.0 will solve this.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Mattias Sjögren <ma************ ********@mvps.o rg> wrote:
All the event handlers seem to pass an Object and an EventArgs object.
If the event doesn't need this info why pass them anyway? It is
inefficient.
It's consistent with other event signatures and the .NET design
guidelines.

http://msdn.microsoft.com/library/en...pconEventNamin
gGuidelines.asp
http://msdn.microsoft.com/library/en...pconEventUsage
Guidelines.asp

It's not like you're wasting significant clock cycles by passing an
extra parameter, especially if you just pass in EventArgs.Empty .


Unfortunately I think the convention is somewhat broken. For anything
that really uses a subclass of EventArgs (and let's face it, EventArgs
itself is pretty useless on its own) you want to use a more strongly
typed event handler - such as KeyPressEventHa ndler. Given that, why
specify EventArgs when there's nothing useful?

I must admit this is one part of the convention that I sometimes break
- I prefer to use

// No more information
void FooEventHandler (object sender);
// Strongly typed information
void BarEventHandler (object sender, BarEventArgs e);

I don't see the point of including a needless parameter - and the fact
that it's there sort of implies that there might be something useful
about it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Isn't it because events are implemented within C# .NET as a multicast
delegate, and therefore are required to all have the same signature? The
EventArgs is intended to be subclassed in case you actually need to pass
data. Sure it causes a little confusion until you're used to it, but there
really isn't a performance penalty in sending an arg that carries no
information.

-Rachel

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:u8******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

I also agree with Jon, EventArgs is useless. and I also break it all the
time.

One particular escenario when I'd see this happen is when a worker thread need to signal the UI thread that it's done. All it needs is a method that
is executed in the main thread, it does not matter the sender, and even less an arg.
Fortunely I think that the anonymous methods in C# 2.0 will solve this.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Mattias Sjögren <ma************ ********@mvps.o rg> wrote:
All the event handlers seem to pass an Object and an EventArgs object.
If the event doesn't need this info why pass them anyway? It is

inefficient.

It's consistent with other event signatures and the .NET design
guidelines.

http://msdn.microsoft.com/library/en...pconEventNamin
gGuidelines.asp
http://msdn.microsoft.com/library/en...pconEventUsage
Guidelines.asp

It's not like you're wasting significant clock cycles by passing an
extra parameter, especially if you just pass in EventArgs.Empty .


Unfortunately I think the convention is somewhat broken. For anything
that really uses a subclass of EventArgs (and let's face it, EventArgs
itself is pretty useless on its own) you want to use a more strongly
typed event handler - such as KeyPressEventHa ndler. Given that, why
specify EventArgs when there's nothing useful?

I must admit this is one part of the convention that I sometimes break
- I prefer to use

// No more information
void FooEventHandler (object sender);
// Strongly typed information
void BarEventHandler (object sender, BarEventArgs e);

I don't see the point of including a needless parameter - and the fact
that it's there sort of implies that there might be something useful
about it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Rachel Suddeth <ra****@bldhoun d.com> wrote:
Isn't it because events are implemented within C# .NET as a multicast
delegate, and therefore are required to all have the same signature? The
EventArgs is intended to be subclassed in case you actually need to pass
data.
Except not all events *do* have the same signature. Every delegate
which actually carries real information has a different signature, such
as:

public delegate void KeyPressEventHa ndler(
object sender,
KeyPressEventAr gs e
);

Note that you can't use a "compatible " delegate - you can't declare all
your methods with

public void Foo(object sender, EventArgs e)

and then do

....KeyPress += new KeyPressEventHa ndler (Foo);

because Foo doesn't have the same signature.
Sure it causes a little confusion until you're used to it, but there
really isn't a performance penalty in sending an arg that carries no
information.


There's a readability penalty though - it implies there's information
there when there isn't.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hi,

"Rachel Suddeth" <ra****@bldhoun d.com> wrote in message
news:ud******** *****@TK2MSFTNG P12.phx.gbl...
Isn't it because events are implemented within C# .NET as a multicast
delegate, and therefore are required to all have the same signature?
For a particular event yes, not for all events. Each event may(and do) has
its particular signature
The
EventArgs is intended to be subclassed in case you actually need to pass
data. Sure it causes a little confusion until you're used to it, but there
really isn't a performance penalty in sending an arg that carries no
information.


That is part of the problem. as you MUST subclass EventArgs you cannot use
the same signature for all events. hence the pointless of having EventArgs
in the first place to indicate an event with no parameters.

It does not have any big performance penalty , but here we are talking
about design , theory if you want :) .

What strikes me now , that we are talking about it is that it seems that MS
decided to adopt two different approach in the suggested event signature,
you have the sender, which being an object you must cast it to a particular
type if you want to use it for something, even as I believe it's less used
than EventArgs. But instead of using the same approach for the data
associated with the event ( using an object instance ) it create a new base
class for that, base class that I find have no more use that object in the
same place.

It does have its logic , the sender can be anything, so you can't do
anything else that makes it an object and delegate the responsability to
casting or determine the type to the implementators of the handler. but the
same may apply to the data object. in this case what I would have done is
create an EventArgs class too, but I would make it abstract . this will make
prevent the creation of instances of it.
Now that I finished the message I think that somehow I forget what I was
thinking when I started it and what I was going to say :) It's mid morning a
nd I need caffeine , I will make an expresso to wake me up :D

Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #7
Thanks, I see (sort of.)

I had this picture of some part of the supporting structure doing something
generic with everything that took an Object & and Eventargs and no return,
but obviously that's not the case since you can do it without those and it
works. And I think I do see what you were getting at...

There's so much abstraction here I think I'll never be able to understand
(but patience... I haven't been dealing with long... just seems like by the
time I finally get it, it'll be obsolete, eh?) Nobody shoot me if I miss
dealing with things I could understand like a message loop with a big fat
switch in it ... <sigh> I know this makes working with GUI easier, only
wish I could understand what was going on.

--Rachel

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

"Rachel Suddeth" <ra****@bldhoun d.com> wrote in message
news:ud******** *****@TK2MSFTNG P12.phx.gbl...
Isn't it because events are implemented within C# .NET as a multicast
delegate, and therefore are required to all have the same signature?
For a particular event yes, not for all events. Each event may(and do)

has its particular signature
The
EventArgs is intended to be subclassed in case you actually need to pass
data. Sure it causes a little confusion until you're used to it, but there really isn't a performance penalty in sending an arg that carries no
information.
That is part of the problem. as you MUST subclass EventArgs you cannot

use the same signature for all events. hence the pointless of having EventArgs
in the first place to indicate an event with no parameters.

It does not have any big performance penalty , but here we are talking
about design , theory if you want :) .

What strikes me now , that we are talking about it is that it seems that MS decided to adopt two different approach in the suggested event signature,
you have the sender, which being an object you must cast it to a particular type if you want to use it for something, even as I believe it's less used
than EventArgs. But instead of using the same approach for the data
associated with the event ( using an object instance ) it create a new base class for that, base class that I find have no more use that object in the
same place.

It does have its logic , the sender can be anything, so you can't do
anything else that makes it an object and delegate the responsability to
casting or determine the type to the implementators of the handler. but the same may apply to the data object. in this case what I would have done is
create an EventArgs class too, but I would make it abstract . this will make prevent the creation of instances of it.
Now that I finished the message I think that somehow I forget what I was
thinking when I started it and what I was going to say :) It's mid morning a nd I need caffeine , I will make an expresso to wake me up :D

Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 16 '05 #8
Hi rachel,

If you keep around here reading you will learn ALOT , I learn something
almost daily.

btw , the big fat switch is still around , it's just that you do not see it
anymore :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Rachel Suddeth" <ra****@bldhoun d.com> wrote in message
news:O9******** *****@TK2MSFTNG P09.phx.gbl...
Thanks, I see (sort of.)

I had this picture of some part of the supporting structure doing something generic with everything that took an Object & and Eventargs and no return, but obviously that's not the case since you can do it without those and it
works. And I think I do see what you were getting at...

There's so much abstraction here I think I'll never be able to understand
(but patience... I haven't been dealing with long... just seems like by the time I finally get it, it'll be obsolete, eh?) Nobody shoot me if I miss
dealing with things I could understand like a message loop with a big fat
switch in it ... <sigh> I know this makes working with GUI easier, only
wish I could understand what was going on.

--Rachel

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,

"Rachel Suddeth" <ra****@bldhoun d.com> wrote in message
news:ud******** *****@TK2MSFTNG P12.phx.gbl...
Isn't it because events are implemented within C# .NET as a multicast
delegate, and therefore are required to all have the same signature?
For a particular event yes, not for all events. Each event may(and do)

has
its particular signature
The
EventArgs is intended to be subclassed in case you actually need to pass data. Sure it causes a little confusion until you're used to it, but there really isn't a performance penalty in sending an arg that carries no
information.


That is part of the problem. as you MUST subclass EventArgs you cannot

use
the same signature for all events. hence the pointless of having EventArgs in the first place to indicate an event with no parameters.

It does not have any big performance penalty , but here we are talking
about design , theory if you want :) .

What strikes me now , that we are talking about it is that it seems that MS
decided to adopt two different approach in the suggested event
signature, you have the sender, which being an object you must cast it to a

particular
type if you want to use it for something, even as I believe it's less used than EventArgs. But instead of using the same approach for the data
associated with the event ( using an object instance ) it create a new

base
class for that, base class that I find have no more use that object in the same place.

It does have its logic , the sender can be anything, so you can't do
anything else that makes it an object and delegate the responsability to
casting or determine the type to the implementators of the handler. but

the
same may apply to the data object. in this case what I would have done is create an EventArgs class too, but I would make it abstract . this will

make
prevent the creation of instances of it.
Now that I finished the message I think that somehow I forget what I was thinking when I started it and what I was going to say :) It's mid

morning a
nd I need caffeine , I will make an expresso to wake me up :D

Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 16 '05 #9

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

Similar topics

1
2172
by: Al | last post by:
Ref:how to pass value along with click of a button hi,I have created a form that has single button. as follow <asp:button id="btnCallfunction" runat="server" text="func_call" onclick="add_int" /> Sub func_call(Source As Object, E As EventArgs) ' how to capture the passed variable here end Sub
3
25283
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a user control to invoke a method in the parent form. This code doesn't seem to work for me, I've tried it a number of times with very simple test cases (A user control with a single button and a parent form with a single text box) and it always...
8
3365
by: Brian F | last post by:
Exactly what the subject says. I have an ASP page that I want my C# windows application to open in Internet Explorer, and if possible have it send along a list of values from a list box. Thank you.
1
4676
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event, if you set this ActionCode to various values the control will respond in various ways (reject, reject change field, accept, etc). I am trying to understand exactly how that works as I am trying to extend the control and add more processing s...
7
8485
by: SB | last post by:
What is the proper way to pass a character array (char *) from a "C" dll to a C# method (delegate) in my app? Getting the dll (which simulates a third party dll) to call my delegate works fine. However, as soon as I try to add any "char *" parameters, I start getting exceptions in my C# app. So far I've tried (among others): public delegate int Test(char version); and public delegate int Test(string version); // won't work because...
3
30905
by: Amil | last post by:
when i double-click on a button on a form as i am designing the form, the IDE creates a method corresponding to a event assignment as follows: this eventhandler: this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); is paired with: private void exitToolStripMenuItem_Click(object sender, EventArgs e) {} How do/Can I what the EventArgs are?
3
2231
by: sloan | last post by:
How does one "pass thru" a Raised Event.... I am using the Adapter Pattern to sync up some different interfaces. http://www.dofactory.com/Patterns/PatternAdapter.aspx My Question is this:
24
55213
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new EventHandler(Onbutton_click); I want to pass more information related that event. & want to use that
12
11098
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
8889
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.