AddHandler/RemoveHandler question

  1. The Xojo docs show this:

AddHandler MyTimer.Action, WeakAddressOf TimerAction

Is this also OK?:

AddHandler MyTimer.Action, WeakAddressOf Self.TimerAction

Is there ever any reason to add the “Self.” to the AddHandler/RemoveHandler? Does it cause harm?

  1. If the handler exists in another module is this OK?:

AddHandler MyTimer.Action, WeakAddressOf OtherModule.TimerAction

AddHandler/RemoveHandler need an unambiguous reference to the intended method. If “Self” helps do that, it’s fine. If you include “Self” and didn’t really need it, that’s fine too. This is true when referencing any method or property.

You can reference a method from a module in those calls, but you cannot use “WeakAddressOf”, you must use “AddressOf”. Modules are ever-present and do not need or use reference counts, the thing the Weak address is meant to combat. In fact, the compiler won’t even let you do it when you try to build.

The same is true for Shared Methods in a class.

Thanks Kem,

I was just trying to work out in my mind how these things work. I see what you mean about modules being ever-present. I was thinking more in the line of can I reference a handler that isn’t in my current class/window etc. by employing a fully qualified name. I take it that the compiler always looks first for the handler name in the current file (class/window etc.)?

I wonder if the compiler is smart enough to catch mis-matched AddressOf/WeakAddressOf pairings like this?:

AddHandler MyTimer.Action, WeakAddressOf TimerAction

Followed by:

RemoveHandler MyTimer.Action, AddressOf TimerAction

[quote=452428:@Wes Westhaver]I wonder if the compiler is smart enough to catch mis-matched AddressOf/WeakAddressOf pairings like this?:

[/quote]
The compiler isn’t, but you’ll receive an exception if you have more RemoveHandlers than AddHandler.

It is a life goal to never find out. :slight_smile: