473,388 Members | 1,352 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,388 software developers and data experts.

Confussed about parameters for Events

In one of my books called "Mastering C#" there is a statement that reads
"All event handler delegates must return void and accept two parameters. The
first parameter is an object, and it represents the object that raises the
event... The second is a parameter that is an object of a class derived from
the System.EventArgs class".

Now I know that this can not be true in general because I have created
events that do not match these parameters. I know there are some system
delegates that are designed to be used with event handers that are defined
with this signiture and using one of those would require the appropriate
parameters to be passed to it.

I just though maybe I was missing something and thought if anyone can
clarify things, that would be great!.

Thanks for your help

Earl
Nov 15 '05 #1
4 1266
The book is either in error or out of context...

(almost?) all of the event handler delegates in the FCL use this signature,
but it is not a requirement -- rather it is a guideline.

public delegate void SomeEventHandler( object sender,
MyClassThatInheritsFromEventArgs e);
....
public event SomeEventHandler SomeEvent;

This is the preferred pattern for events. However, it's not required. Just
as valid would be
public delegate int SomeEventHandler( int x, int y);
....
public event SomeEventHandler SomeEvent;

Now, ignoring statndards and best practices is done at your own risk, but
there's no technical reason you can't do the last one.

The nice thing about using the first example is that if someone didn't care
for the extended information in the second parameter, they could use
System.EventHandler delegates and hook up to your events blindly. But
again, not a requirement, just a strong guideline.
"news.microsoft.com" <ea******@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
In one of my books called "Mastering C#" there is a statement that reads
"All event handler delegates must return void and accept two parameters. The first parameter is an object, and it represents the object that raises the
event... The second is a parameter that is an object of a class derived from the System.EventArgs class".

Now I know that this can not be true in general because I have created
events that do not match these parameters. I know there are some system
delegates that are designed to be used with event handers that are defined
with this signiture and using one of those would require the appropriate
parameters to be passed to it.

I just though maybe I was missing something and thought if anyone can
clarify things, that would be great!.

Thanks for your help

Earl

Nov 15 '05 #2
Philip, thanks for the reply. I read the entire chapter so I don't think I
got anything out of context. Overall Mastering C# is a very good and easy to
understand book but I guess the author missed on this one. No more
confussion on my part. Thanks

Earl

"Philip Rieck" <st***@mckraken.com> wrote in message
news:Oj**************@TK2MSFTNGP09.phx.gbl...
The book is either in error or out of context...

(almost?) all of the event handler delegates in the FCL use this signature, but it is not a requirement -- rather it is a guideline.

public delegate void SomeEventHandler( object sender,
MyClassThatInheritsFromEventArgs e);
...
public event SomeEventHandler SomeEvent;

This is the preferred pattern for events. However, it's not required. Just as valid would be
public delegate int SomeEventHandler( int x, int y);
...
public event SomeEventHandler SomeEvent;

Now, ignoring statndards and best practices is done at your own risk, but
there's no technical reason you can't do the last one.

The nice thing about using the first example is that if someone didn't care for the extended information in the second parameter, they could use
System.EventHandler delegates and hook up to your events blindly. But
again, not a requirement, just a strong guideline.
"news.microsoft.com" <ea******@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
In one of my books called "Mastering C#" there is a statement that reads
"All event handler delegates must return void and accept two parameters.

The
first parameter is an object, and it represents the object that raises the event... The second is a parameter that is an object of a class derived

from
the System.EventArgs class".

Now I know that this can not be true in general because I have created
events that do not match these parameters. I know there are some system
delegates that are designed to be used with event handers that are defined with this signiture and using one of those would require the appropriate
parameters to be passed to it.

I just though maybe I was missing something and thought if anyone can
clarify things, that would be great!.

Thanks for your help

Earl


Nov 15 '05 #3
Earl,
As Philip suggested it is a guideline, that is worth following.

For details on the Guideline see:

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

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

Hope this helps
Jay

"news.microsoft.com" <ea******@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
In one of my books called "Mastering C#" there is a statement that reads
"All event handler delegates must return void and accept two parameters. The first parameter is an object, and it represents the object that raises the
event... The second is a parameter that is an object of a class derived from the System.EventArgs class".

Now I know that this can not be true in general because I have created
events that do not match these parameters. I know there are some system
delegates that are designed to be used with event handers that are defined
with this signiture and using one of those would require the appropriate
parameters to be passed to it.

I just though maybe I was missing something and thought if anyone can
clarify things, that would be great!.

Thanks for your help

Earl

Nov 15 '05 #4
Jay,

I have normally used these conventions because the books I have read have
done them this way, but it is great to have the official recommedations...
Thanks...

Earl
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl...
Earl,
As Philip suggested it is a guideline, that is worth following.

For details on the Guideline see:

http://msdn.microsoft.com/library/de...Guidelines.asp
http://msdn.microsoft.com/library/de...Guidelines.asp
Hope this helps
Jay

"news.microsoft.com" <ea******@hotmail.com> wrote in message
news:uC**************@TK2MSFTNGP12.phx.gbl...
In one of my books called "Mastering C#" there is a statement that reads
"All event handler delegates must return void and accept two parameters.

The
first parameter is an object, and it represents the object that raises the event... The second is a parameter that is an object of a class derived

from
the System.EventArgs class".

Now I know that this can not be true in general because I have created
events that do not match these parameters. I know there are some system
delegates that are designed to be used with event handers that are defined with this signiture and using one of those would require the appropriate
parameters to be passed to it.

I just though maybe I was missing something and thought if anyone can
clarify things, that would be great!.

Thanks for your help

Earl


Nov 15 '05 #5

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

Similar topics

5
by: someone | last post by:
On page 106 of the second edition of "The C Programming Language" by K & G, two stack operations PUSH n POP are given. >> PUSH *p++ = val; >>POP val = *--p; What I know about stacks is...
3
by: someone | last post by:
On page 106 of the second edition of "The C Programming Language" by K & G, two stack operations PUSH n POP are given. >> PUSH *p++ = val; >>POP val = *--p; What I know about stacks is...
33
by: C# Learner | last post by:
Note ---- Please use a fixed-width font to view this, such as Courier New. Problem
5
by: Martin Bischoff | last post by:
Hi, is it possible to modify the values of a SqlDataSource's select parameters in the code behind before the select command is executed? Example: I have an SqlDataSource with a...
18
by: **Developer** | last post by:
I always define events with the parameters ByVal sender As Object, ByVal e As EventArgs Even if they are not used. Seems I read someplace that's the thing to do. So I then do:
2
by: surjamankhatiwora | last post by:
Can you please give me a brief difference between the vb commends like End,Unload me and Me.hide with its example each...coz while coding, i m totally confussed about these commends....
7
by: Andrus | last post by:
I noticed that DataGridView CellValidated() and other event handler first parameter is object: Grid.CellValidated+=new DataGridViewCellEventHandler(Grid_CellValidated); .... void...
24
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.