Reputation: 1245
A question that has come to my mind. Let's say I want to pass a variable theVar to the event handler declaration below
new EventHandler<AsyncCompletedEventArgs>(evHandler)
and receive it in the definition below:
public void evHandler(object sender, EventArgs e)
{
}
How do I proceed?
Upvotes: 0
Views: 2822
Reputation: 13965
If you are the person writing the code that's raising the event, Mr. Hopkinson puts it very nicely in his answer. You need a custom EventArgs with properties to represent the data you hope to pass.
But if you are merely consuming the event rather than raising it, your options are more limited.
Since you're anticipating receiving an AsyncCompletedEventArgs from your event, though, you do have one option: the UserState propery of AsyncCompletedEventArgs. This is a property you get to supply when you call the asynchronous method that ultimately causes the event to be fired. It can be any object you choose. You supply it when you call the async method, and the event returns it to you in the event argument properties when the method call completes.
Upvotes: 2
Reputation: 20320
Define a descendant of EventArgs
e.g.
public class MySpecialEventArgs :EventArgs
{
public int theVar {get; private set;}
public MySpecialEventArgs(int argVar)
{
theVar = argVar;
}
}
Then when you raise the event throw one of the above in instead of a EventArgs
When you add your handler e will be a MySpecialEventArgs.
Upvotes: 1