Operator

# WeakAddressOf

<div class="rst-class">

forsearch

</div>

Operator

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Creates a `Delegate</api/data_types/additional_types/delegate>`, just like `AddressOf</api/language/addressof>` does, with the difference that it references the method's object instance using a WeakRef internally.

## Usage

``` xojo
delegate = WeakAddressOf methodName
```

| Part                                                  | Description                                                                                                                                                                                                              |
|-------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `Delegate</api/data_types/additional_types/delegate>` | A `delegate</api/data_types/additional_types/delegate>` that references *methodName*. The `Delegate</api/data_types/additional_types/delegate>` automatically converts to a `Ptr</api/data_types/additional_types/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</api/language/addressof>` for those).

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

WeakAddressOf and `AddressOf</api/language/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:

``` xojo
MyTimer As Timer
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

<div class="note">

<div class="title">

Note

</div>

This example is using a `DesktopProgressBar</api/user_interface/desktop/desktopprogressbar>` which has slightly different properties than a `MobileProgressBar</api/user_interface/mobile/mobileprogressbar>` or `WebProgressBar</api/user_interface/web/webprogressbar>`.

</div>

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:

``` xojo
MyTimer = New Timer
MyTimer.Period = 1000
MyTimer.RunMode = Timer.RunModes.Multiple

AddHandler MyTimer.Action, WeakAddressOf TimerAction
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now add the TimerAction method to the layout:

``` xojo
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

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`MemoryBlock</api/language/memoryblock>` function; `AddHandler</api/language/addhandler>`, `AddressOf</api/language/addressof>`, `Declare</api/language/declare>` statement; `Delegate</api/data_types/additional_types/delegate>`, `Ptr</api/data_types/additional_types/ptr>` data types; `Operator Precedence</api/language/operators/operator_precedence>`.

</div>
