Delegate Declaration Guidelines

  • Thread starter Thread starter C# Learner
  • Start date Start date
C

C# Learner

I can't find any guidelines for delegate declarations.

I'm using the following, and welcome any suggestions.

(MyClass is a class declared by me.)

delegate void MyClass[more-detailed name]EventHandler;

e.g.:

delegate void MyClassDisconnectEventHandler;
delegate void MyClassSomethingElseEventHandler;

Is this the recommended thing? Or is it recommended to simply make one,
called MyClassEventHandler, which can pass any relevant argument through
a MyClassEventArgs parameter?
 
Greets,

From the documentation I've seen with regards to delegates and events, I
have typically seen event delegates (notwithstanding those in the .NET
framework) declared as follows:

public delegate void SomeEventHandler(object sender, SomeEventArgs
eventArgs);

The event is typically declared as:

public event SomeEventHandler SomeEvent;

The "SomeEventArgs" is derived from EventArgs and contains information
specific to that event being fired. The "sender" is typically the 'this'
pointer of the object which fired the event.

If you are referring to non-event delegate declarations, just declare
them according to the signature which is required by your "callback"
function. For example, the ThreadStart delegate used by the Thread class
which is used to define the delegate for the thread's entry point.

Regards,

Joe
 
Hi C# Learner,
There are event naming guidelines

1 Pick a name for the event. The name usually has a verb on it
e.g, SelectedItemChanged

2. Declare a delegate called after the event with EventHandler suffix
e.g. SelectedItemChangedEventHandler

3. Declare event's data class that inherit from EventArgs class and is named
after the event with EventArgs suffix
e.g. SelectedItemChangedEventArgs

For more info look at MSDN
MSDN off-line
ms-help://MS.MSDNQTR.2003FEB.1033/cpgenref/html/cpconeventnamingguidelines.h
tm
MSDN on-line
http://msdn.microsoft.com/library/d.../cpgenref/html/cpconeventnamingguidelines.asp


More naming and usage guide lines:
MSDN off-line
ms-help://MS.MSDNQTR.2003FEB.1033/cpgenref/html/cpconnamingguidelines.htm
MSDN on-line
http://msdn.microsoft.com/library/d.../cpgenref/html/cpconeventnamingguidelines.asp
 
C# Learner wrote:

<snip>

Thanks for the replies; it all makes sense now.

However, since my class can raise so many different types of events, I
think I'll just make a single delegate, with a EventArgs subclass that
can handle all relevant parameters.

I know now that it's not recommneded, but it makes life difficult for
the user of my class when there are 10 different delegates used for its
event handlers.
 
Back
Top