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 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
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
That helps a lot--I didn't know there was an EventHandler Generic Delegate!
Thanks.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |