Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Coding Practice Question - Usage of System.EventArgs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frankie - 04 Sep 2007 20:30 GMT
The .NET Framework provides us with built-in event handlers:
   System.EventHandler
and the generic
  System.EventArgs<TEventArgs>

It appears that those built-in event handler delegates are just a
convenience for us, as we can use those instead of creating a new delegate
for every event that we implement.

Question: Is it good to use those built-in event handlers as a matter of
course - and not create a new delegate for our events - provided that the
standard event handler signature [object sender, EventArgs e] will suffice?

Is there any advantage to creating a new delegate _even when_ the built-in
ones listed above would suffice?

Thanks.
Nicholas Paldino [.NET/C# MVP] - 04 Sep 2007 20:40 GMT
Frankie,

   I don't see any.  There really isn't a difference between a delegate
signature of:

delegate void MyEventHandler(object sender, MyEventArgs args);

   And using EventHandler<MyEventArgs>.  They will do the same thing, and
there isn't any real difference between them.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> The .NET Framework provides us with built-in event handlers:
>    System.EventHandler
[quoted text clipped - 14 lines]
>
> Thanks.
Alberto Poblacion - 04 Sep 2007 20:43 GMT
> The .NET Framework provides us with built-in event handlers:
>    System.EventHandler
[quoted text clipped - 12 lines]
> Is there any advantage to creating a new delegate _even when_ the built-in
> ones listed above would suffice?

  In all the code I've seen, the standard practice is to use
System.EventArgs whenever it is sufficient, that is when you do not need to
pass any additional data inside the argument.

  The only advantage that I can think for using a different type of
delegate is that it would leave room for future expansion, meaning that you
could write lots of code using YourEventArgs and if you needed to modify the
event in the future to include an e.something in your event arguments you
would not need to modify all the existing code (except those parts that
needed to make some use of the new fields). But I haven't seen any code that
follows this practice, so I guess that it is not frequent at all to find
yourself in this situation (having to add some data to existing eventargs).
Peter Bromberg [C# MVP] - 05 Sep 2007 01:24 GMT
I think the primary reason for EventHandler is that the signature is so
common throughout the .NET Framework that it makes sense to have a predefined
one. With EventArgs (which is not really an "event handler", but a separate
class) this provides the base class from which all more complex EventArgs
functionality is derived, even though in many cases it serves no purpose ,
e.g., "EventArgs.Empty"
-- Peter
Recursion: see Recursion
site:  http://www.eggheadcafe.com
unBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder:    http://www.blogmetafinder.com

> The .NET Framework provides us with built-in event handlers:
>     System.EventHandler
[quoted text clipped - 13 lines]
>
> Thanks.
Frankie - 05 Sep 2007 02:12 GMT
RE: << EventArgs (which is not really an "event handler>>

geeze! I meant to put System.EventHandler in the subject of the OP..... not
EventArgs... hopefully the actual question I posed there made it clear what
I was inquiring about....

>I think the primary reason for EventHandler is that the signature is so
> common throughout the .NET Framework that it makes sense to have a
[quoted text clipped - 30 lines]
>>
>> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.