Gets the address of a method (as a delegate). Can be used with AddHandler, Declares and other advanced commands.
delegate = AddressOf 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:
MyTimer As Timer
Next, add a ProgressBar to the layout. The Timer will update the ProgressBar.
In the Opening 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 Timer MyTimer.Period = 1000 MyTimer.Mode = Timer.Modes.Multiple AddHandler MyTimer.Action, AddressOf TimerAction
Now add the TimerAction method to the layout:
Sub TimerAction(sender As Timer) If ProgressBar1.CurrentValue < ProgressBar1.MaxValue Then ProgressBar1.CurrentValue = ProgressBar1.CurrentValue + 1 Else // Stop Timer and Remove the handler sender.Mode = 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.
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)
All project types on all supported operating systems.