Please see my comment to the question. As you need to pass some data to event handler, and not to the event (not clear what would it possibly mean), the problem is pretty trivial.
There are two approaches to passing of the data to a handler. First of all, you should decide if you are using a named method for an event handler, or an anonymous one.
Let's start with some "regular", named method. For simplicity, let's assume you need to handle some button
Click
events. In the context of this problem, the type of an event arguments and event handler does not really matter. Let's see:
class SomeClass {
void SomeSetupMethod() {
MyButton.Click += MyButtonClickHandler;
}
void SomeSetupMethod(object sender, System.EventArgs eventArgs) {
if (id != null)
}
string id;
}
As you can see, in this context, when you have a method used as a event handler and some member you want to use to pass data to a handler are the members of the same class, you can freely use any class members (such as
id
you are interested in) in the implementation of the handler. The question is: why? The answer is: because you can use an instance (non-static) method as a handler. An instance method has an additional parameter "this" passed to provide the access to all instance (non-static) members of some instance of a class, actually, "this" is a reference to a particular instance of the class, the one used at the call. In the operator "+=" the operand
MyButtonClickHandler
represents the delegate instance which passes both address of the method to be called, with the particular "this" reference. This delegate instance object is copied to the
invocation list
of the event instance, which guaranteed a correct call. With right "this", and, hence, indirectly, with right value of
id
.
Another method is related to the
anonymous delegates. The mechanism of passing parameter is much harder to understand in this case. It is related to the notion of
closure. So, first of all, try to understand closures:
http://en.wikipedia.org/wiki/Closure_%28computer_science%29[
^].
This is not the best place to explain how closures work in your case. It could be a subject of a whole article. I'll just give you the idea how it might look in code:
class SomeClass {
void SomeSetupMethod() {
MyButton.Click += delegate(object sender, System.EventArgs eventArgs) {
if (id != null)
};
}
string id;
}
Also, if you use the .NET Framework version newer than v.2.0, which is usually the case, you can also use lambda syntax, which nicely simplifies writing the anonymous methods, and not only event handlers:
void SomeSetupMethod() {
MyButton.Click += (sender, eventArgs) => {
if (id != null)
};
}
In this syntax, you don't have to write exact argument types (which is especially good because they are often unused); their types are determined by the compiler using
type inference from the known event type. Please see:
http://en.wikipedia.org/wiki/Type_inference[
^],
http://msdn.microsoft.com/en-us/vstudio/jj131514.aspx[
^].
You follow-up questions are welcome.
—SAP.S.: Sorry, I've written it all in C# because I forgot you used C++ while I was writing this text, and writing in C++ would take some more of my time. I hope this is enough for you to get the idea. If you cannot figure out how to express similar things in C++, please ask me another question, I'll try to help.