Check if AddHandler has been implemented

I’m guessing not, but is there any way to check an event to see if it has a handler? Something like:

if myObject.myEvent not HasHandler then 
  AddHandler myObject.myEvent WeakAddressOf myMethod
end if

Thanks.

You can do this with a Try…Catch block. Here’s an example to get you started:

[code]myTimer = new timer
myTimer.Mode = 2
AddHandler myTimer.action, AddressOf Timer_Action

try
AddHandler myTimer.Action, AddressOf Timer_Action
RemoveHandler myTimer.action, AddressOf Timer_action
catch
MsgBox( “Has Handler!” )
end try
[/code]

If the Timer instance’s Action event already has an assigned handler (which it does, according to Line 3) you’ll get the “Has Handler” messagebox, as trying to assign a new handler throws a RuntimeException.

There may be some way to determine this with some introspection voodoo, but that’s not my forte.

Thank you. I had tried that, and it works, sort of. I was looking for a way to avoid having the debugger break at that point.

Yeah, short of disabling “Break on Exceptions” in the Project menu, I don’t believe you can.

myTimer = new timer
myTimer.Mode = 2
AddHandler myTimer.action, AddressOf Timer_Action

#Pragma BreakOnExceptions False
try
  AddHandler myTimer.Action, AddressOf Timer_Action
  RemoveHandler myTimer.action, AddressOf Timer_action
catch
  MsgBox( "Has Handler!" )
end try

Bah! I always forget about that pragma! Well done, Tim!

Aha! Right there under our noses! :slight_smile: