From Xojo Documentation
Gets the address of a method (as a delegate). Can be used with AddHandler, Declares and other advanced commands.
delegate = AddressOf methodName
|Delegate||A delegate that references methodName. The Delegate automatically converts to a Ptr.|
|methodName||The name of the method to create a delegate for.|
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.
This example lets you handle the Timer.Action event without creating a Timer subclass.
Start by adding a property to the layout:
Next, add a ProgressBar to the layout. The Timer will simply update the ProgressBar.
|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.Period = 1000
MyTimer.Mode = Xojo.Core.Timer.Modes.Multiple
AddHandler MyTimer.Action, AddressOf TimerAction
Now add the TimerAction method to the layout:
If ProgressBar1.CurrentValue < ProgressBar1.MaxValue Then
ProgressBar1.CurrentValue = ProgressBar1.CurrentValue + 1
// Stop Timer and Remove the handler
sender.Mode = Xojo.Core.Timer.Mode.Off
RemoveHandler MyTimer.Action, AddressOf TimerAction
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:
Dim p As Ptr = IOPSNotificationCreateRunLoopSource(AddressOf IOPowerSourceCallback, Nil)