Information Technology Reference
In-Depth Information
Custom class name Base class
public class MyTCEventArgs: EventArgs
{
public string Message; // Stores a message
public MyTCEventArgs(string s) // The constructor sets the message.
{
Message = s;
}
}
Using the Custom Delegate
Now that you have a custom class for passing data in the second parameter of your event
handlers, you need a delegate type that uses the new custom class. There are two ways you can
do this:
￿
The first way is to use the nongeneric method:
-
Create a new custom delegate, using your custom class type, as shown in the
following code.
-
Use the new delegate name throughout the four other sections of the event code.
Custom delegate name Custom class
public delegate void MyTCEventHandler(object sender, MyTCEventArgs e);
￿
The second way is new with C# 2.0, and uses the generic delegate EventHandler<> , which
is also declared in the namespace System . To use the generic delegate, follow these steps:
-
Place the name of the custom class between the angle brackets.
-
Use the entire string, wherever you would have used the name of your custom
delegate type. For example, this is what the event declaration would look like.
Generic delegate using custom class
public event EventHandler< MyTCEventArgs > Elapsed;
Event name
Use the custom class and the custom delegate, either nongeneric or generic, in the other
four sections of code dealing with the event.
For example, the following code updates the MyTimerClass code to use a custom EventArgs
class called MyTCEventArgs and the generic EventHandler<> delegate.
Search WWH ::




Custom Search