AddressOf

From Xojo Documentation

Operator

Gets the address of a method (as a delegate). Can be used with AddHandler, Declares and other advanced commands.

Usage

delegate = AddressOf methodName

Part Description
Delegate A delegate that references methodName. The Delegate automatically converts to a Ptr.
methodName The name of the method to create a delegate for.

Notes

AddressOf can also be used to provide a callback function to an external library method that has been defined with the Declare statement.

Note that if the method is an instance method of a class, the returned delegate will also retain a reference to the class object. This can, in some cases, lead to circular references, causing memory leaks if they’re not explicitly broken up after use. To avoid this complication, consider using WeakAddressOf, which will reference the class object via a WeakRef, thereby avoiding the side effects of circular referencing.

AddressOf and WeakAddressOf are operators, not functions, and cannot be used with parentheses. For example, AddressOf(someMethod) leads to an error. Use AddressOf someMethod instead.

Sample Code

This example lets you handle the Timer.Action event without creating a Timer subclass.

Start by adding a property to the layout:

MyTimer As Xojo.Core.Timer

Next, add a ProgressBar to the layout. The Timer will simply update the ProgressBar.

fa-info-circle-32.png
This example is using an iOSProgressBar which has slightly different properties than a desktop ProgressBar or WebProgressBar.

In the Open event handler of the layout, instantiate the Timer and indicate that its Action event handler should be handled by a method, TimerAction, that you will add to the layout:

MyTimer = New Xojo.Core.Timer
MyTimer.Period = 1000
MyTimer.Mode = Xojo.Core.Timer.Modes.Multiple

AddHandler MyTimer.Action, AddressOf TimerAction

Now add the TimerAction method to the layout:

Sub TimerAction(sender As Xojo.Core.Timer)
If ProgressBar1.CurrentValue < ProgressBar1.MaxValue Then
ProgressBar1.CurrentValue = ProgressBar1.CurrentValue + 1
Else
// Stop Timer and Remove the handler
sender.Mode = Xojo.Core.Timer.Mode.Off
RemoveHandler MyTimer.Action, AddressOf TimerAction
End If
End Sub

Remember that the first parameter to TimerAction must be of the type of the original object, in this case a Timer.

When you run the project, the ProgressBar is updated once per second.

Passing a method's address to an OS function

Here, the macOS function IOPSNotificationCreateRunLoopSource expects to get passed a function pointer that is invoked later by the OS. You declare a shared or global method like Sub IOPowerSourceCallback(context as Ptr) and pass it to the function like this:

Declare Function IOPSNotificationCreateRunLoopSource Lib IOKit (callback As Ptr, context As Ptr) As Ptr
Var p As Ptr = IOPSNotificationCreateRunLoopSource(AddressOf IOPowerSourceCallback, Nil)

See Also

MemoryBlock function; Declare statement; Delegate, Ptr data types; Operator Precedence; WeakAddressOf; AddHandler.