I have a case where I want a given event to execute once, and only once. I'm trying to do it this way, but I'm having prolems (else I wouldn't be asking). Note that the following code is inside the function that decides that the event needs to be fired.
EventHandler h = delegate(Object sender, EventArgs e) {
FiringControl.TheEvent -= h; // <-- Error, use of unassigned local variable "h"
// Do stuff
}
FiringControl.TheEvent += h;
In general this should work because of the way scope is preserved for delegates until after they're done running, but, since h
is in the process of being built when I try to use it it is still considered to be "uninitialized", apparently.