By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,203 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,203 IT Pros & Developers. It's quick & easy.

Generic event args in delegate?

P: n/a
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

Apr 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David Veeneman" <da****@nospam.com (domain is my last name)> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
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


Apr 3 '06 #2

P: n/a
public delegate void ItemChangingEventHandler<T>(object sender,
ItemChangingEventArgs<T> e);

"David Veeneman" <da****@nospam.com (domain is my last name)> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
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


Apr 3 '06 #3

P: n/a
That helps a lot--I didn't know there was an EventHandler Generic Delegate!
Thanks.
Apr 3 '06 #4

P: n/a
Thanks!
Apr 3 '06 #5

P: n/a
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.
Apr 3 '06 #6

P: n/a
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.
Apr 7 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.