HI,
I have a button click event where a user defined type is initialised, and an event is set-up.
This button is used numerous times, and I don’t want the events to stack. Therefore I have unsubscribed to the event in the finally block.
The code is similar to below:
try {
bar = new foo();
bar.event += new event(method);
dosomething()
}
finally {
bar.event -= new event(method);
}
It seems to work okay so far, however I’m concerned with the finally block being processed before the doSomething method has completed and thus made use of the event.
Would the method be allowed to process before the finally block is called?
The finally block will execute, when
In the last case, it will access the uninitialized variable bar and throw a NullReferenceException. So you better place the
bar = new foo();before the try block.