If I am assigning an event handler at runtime and it is in a spot that can be called multiple times, what is the recommended practice to prevent multiple assignments of the same handler to the same event.
object.Event += MyFunction
Adding this in a spot that will be called more than once will execute the handler ‘n’ times (of course).
I have resorted to removing any previous handler before trying to add via
object.Event -= MyFunction; object.Event += MyFunction;
This works but seems off somehow. Any suggestions on proper handling 😉 of this scenario.
Baget is right about using an explicitly implemented event (although there’s a mixture there of explicit interface implementation and the full event syntax). You can probably get away with this:
That may have some odd edge cases if you ever add or remove multicast delegates, but that’s unlikely. It also needs careful documentation as it’s not the way that events normally work.