Operator

WeakAddressOf


Description

Creates a Delegate, just like AddressOf does, with the difference that it references the method's object instance using a WeakRef internally.

Usage

delegate = WeakAddressOf methodName

Part

Description

Delegate

A delegate that references methodName. The Delegate automatically converts to a Ptr.

methodName

The method to which you want a pointer.

Notes

It can only be used with instance methods of classes, not with shared methods and global methods from modules (use AddressOf for those).

If the delegate method is invoked after the object has been destroyed, an exception will be raised.

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

Sample code

This example lets you handle the Timer.Action (Desktop & Console projects) 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.

Note

This example is using a DesktopProgressBar which has slightly different properties than a MobileProgressBar or WebProgressBar.

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.RunMode = Timer.RunModes.Multiple

AddHandler MyTimer.Action, WeakAddressOf TimerAction

Now add the TimerAction method to the layout:

Sub TimerAction(sender As Timer)
  If ProgressBar1.Value < ProgressBar1.MaximumValue Then
    ProgressBar1.Value = ProgressBar1.Value + 1
  Else
    ' Stop Timer and Remove the handler
    sender.RunMode = Timer.RunModes.Off
    RemoveHandler MyTimer.Action, WeakAddressOf TimerAction
  End If
End Sub

Compatibility

All project types on all supported operating systems.

See also

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