AddressOf

From Real Software Documentation

Jump to: navigation, search
Operator

Returns a Delegate, which is practically a function pointer, for the passed method.

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.

Syntax

delegate=AddressOf methodName

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

Examples

Passing a method’s address to a OS function

Here, the Mac OS X 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
Dim p As Ptr = IOPSNotificationCreateRunLoopSource(AddressOf IOPowerSourceCallback, Nil)

See Also

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

Personal tools