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

Generic event args in delegate?

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
6 10391
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
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
That helps a lot--I didn't know there was an EventHandler Generic Delegate!
Thanks.
Apr 3 '06 #4
Thanks!
Apr 3 '06 #5
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
18
by: Elder Hyde | last post by:
Hey all, A class of mine needs to tell the outside world when its buffer is not empty. The problem is that C# seems to force you to put the event-raising code in the base class. To illustrate,...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
1
by: Kerry Jenkins | last post by:
I am having problems passing an Event Delegate as an argument to a method that accepts a delegate argument. I get the following error message: 'Public Event ProgressChanged(sender As Object, e...
6
by: utkarsh | last post by:
Hi All, I am using the following method "FireAsync" (i got the following information from the google groups) to fire the event for all the subscribers. Is there another way to fire the event...
7
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
8
by: Advait Mohan Raut | last post by:
Hello, I am using VC# 2005 ; C# 2.0 I am working on the performance measurement tool. For that I need my code to call user defined method along with its parameters. For that should I use a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.