AddHandler/RemoveHandler question

  1. 3 weeks ago

    Wes W

    Sep 1 Pre-Release Testers, Xojo Pro MD, Columbia USA

    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?

    2) If the handler exists in another module is this OK?:

    AddHandler MyTimer.Action, WeakAddressOf OtherModule.TimerAction

  2. Kem T

    Sep 1 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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.

  3. Wes W

    Sep 1 Pre-Release Testers, Xojo Pro MD, Columbia USA

    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

  4. Greg O

    Sep 1 Xojo Inc

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

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

  5. Kem T

    Sep 1 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

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

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

or Sign Up to reply!