Operator

# AddressOf

<div class="rst-class">

forsearch

</div>

Operator

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

## Description

Gets the address of a method (as a delegate). Can be used with AddHandler, Declares and other advanced commands.

## Usage

``` xojo
delegate = AddressOf 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 name of the method to create a delegate for.                                                                                                                                                                         |

## Notes

<span class="title-ref">AddressOf</span> can also be used to provide a callback function to an external library method that has been defined with the `Declare</api/language/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</api/language/weakaddressof>`, which will reference the class object via a `WeakRef</api/language/weakref>`, thereby avoiding the side effects of circular referencing.

<span class="title-ref">AddressOf</span> and `WeakAddressOf</api/language/weakaddressof>` are operators, not functions, and cannot be used with parentheses. For example, *AddressOf(someMethod)* leads to an error. Use *AddressOf 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, AddressOf 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, 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:

``` xojo
Declare Function IOPSNotificationCreateRunLoopSource Lib IOKit (callback As Ptr, context As Ptr) As Ptr
Var p As Ptr = IOPSNotificationCreateRunLoopSource(AddressOf IOPowerSourceCallback, Nil)
```

## Compatibility

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

<div class="seealso">

`MemoryBlock</api/language/memoryblock>` function; `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>`; `WeakAddressOf</api/language/weakaddressof>`; `AddHandler</api/language/addhandler>`.

</div>
