Generic event args in delegate?

D

David Veeneman

I have several events that pass a value in their event args. One event
passes an int, another a string, another a DateTime, and so on. Rather than
creating a separate set of event args for each type, I have created a
generic event args class that looks like this:

public class ItemChangingEventArgs<T>
{
...
}


I want to declare a delegate that uses the generic event args, with the
generic type:

public delegate void ItemChangingEventHandler(object sender,
ItemChangingEventArgs<T> e);

That way, I could specify the type when a delegate is actually created. But
when I declare the delegate, I get the error "The type or namespace name 'T'
could not be found". If I leave off the <t> in the delegate declaration, I
get the error "Using the generic type 'ItemChangingEventArgs<T>' requires
'1' type arguments."

The delegate declaration works if I declare the type of the event args:

public delegate void ItemChangingEventHandler(object sender,
ItemChangingEventArgs<int> e);

I'd like to leave the type as <T> in the delegate declaration, so that I can
declare one delegate, instead of a delegate for each type.

Is there a way to declare a delegate with generic event args, without
specifying the type of the event args? If so, how do I declare the delegate?
Thanks in advance

David Veeneman
Foresight Systems
 
N

Nicholas Paldino [.NET/C# MVP]

David,

I wouldn't declare a delegate for this, just use the generic
EventHandler delegate, and use your ItemChangingEventArgs<T> (where T is
your type) as the event handler. When creating it, you would do something
like this:

EventHandler<ItemChangingEventArgs<string>> handler = <method name here>;

Hope this helps.
 
R

Rene

public delegate void ItemChangingEventHandler<T>(object sender,
ItemChangingEventArgs<T> e);
 
D

David Veeneman

That helps a lot--I didn't know there was an EventHandler Generic Delegate!
Thanks.
 
D

David Veeneman

Here are a couple of the event declarations I eventually used in my classes:

public event EventHandler<ItemChangingEventArgs<DateTime?>> DateChanging;
public event EventHandler<ItemChangingEventArgs<string>> TextChanging;

I didn't have to declare delegates to go along with these events. As
Nicholas Paldino suggested, I used the Generic Delegate with my generic
event args (ItemChangingEventArgs<T>). Very simple, and rather elegant.
 
D

David Veeneman

Update: I discovered that events I declared with the the EventHandler
generic delegate did not appear in the VS 2005 Properties window. So, I
declared a delegate as follows:

public delegate void ItemChangingEventHandler<T>(object sender,
ItemChangingEventArgs<T> e);

My original attempt didn't work because you need the <T> generic type
specifier after the delegate name, as well as after the event args.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top