DataType

# Delegate

<div class="rst-class">

forsearch

</div>

DataType

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

## Description

A <span class="title-ref">Delegate</span> data type is an object representing a specific method.

## Usage

For use in `XojoScript</api/language/xojo_script/xojoscript>` code:

``` xojo
Delegate Sub name (parameterList)
```

or

``` xojo
Delegate Function name (parameterList) As DataType
```

| Part          | Description                                                                                                                                    |
|---------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| name          | Required. The name of the <span class="title-ref">delegate</span>.                                                                             |
| parameterList | Optional: List of values representing parameters that are passed to the method when it is called. Multiple parameters are separated by commas. |
| DataType      | Optional. The data type of the value returned by the function.                                                                                 |

<div class="important">

<div class="title">

Important

</div>

Only Global <span class="title-ref">delegates</span> defined in a Module are currently supported with Android.

</div>

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                 | Parameters                                       | Returns | Shared |
|--------------------------------------|--------------------------------------------------|---------|--------|
| `Constructor<delegate.constructor0>` | p As `Ptr</api/data_types/additional_types/ptr>` |         |        |
| `Invoke<delegate.invoke>`            | \[parameter1 As Type, parameterN As Type\]       | Type    |        |

## Method descriptions

<div id="delegate.constructor0">

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

</div>

<div class="rst-class">

forsearch

</div>

Delegate.Constructor

**Constructor**(p As `Ptr</api/data_types/additional_types/ptr>`)

> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> `Constructors</api/language/constructor>` are special methods called when you create an object with the `New</api/language/new>` keyword and pass in the parameters above.
>
> </div>
>
> The <span class="title-ref">Delegate</span> type's constructor takes a single Ptr, which is assumed to be correct for the <span class="title-ref">Delegate</span> type.
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This Constructor is not supported for Android.
>
> </div>
>
> Assume there is a <span class="title-ref">Delegate</span> declared as SimpleProc:
>
> ``` xojo
> Var pp As Ptr = AnOSFunctionThatReturnsAFunctionPointer()
> Var sp As New SimpleProc(pp)
> sp.Invoke()
> ```

<div id="delegate.invoke">

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

</div>

<div class="rst-class">

forsearch

</div>

Delegate.Invoke

**Invoke**(\[parameter1 As Type, parameterN As Type\]) As Type

> Call the method pointed by the <span class="title-ref">Delegate</span>. You must pass the same parameters as the method's ones and, if the method returns a value, you must use the return value as well.
>
> If you defined a <span class="title-ref">Delegate</span> as:
>
> ``` xojo
> Delegate Sub myDelegateMethod(x As Integer, y As Integer, s As String)
> ```
>
> then you must use Invoke with the same parameters' type, as in:
>
> ``` xojo
> Var d As myDelegateMethod
> ' <— Set d as your code requires
> d.Invoke(1, 10, "An example string") ' Values are indicative
> ```
>
> By contrast, if the <span class="title-ref">Delegate</span> is set to return a value as in:
>
> ``` xojo
> Delegate Function myDelegateMethod(x As Integer, y As Integer, s As String ) As Boolean
> ```
>
> then you must use the value returned by Invoke, as in:
>
> ``` xojo
> Var d As myDelegateMethod
> Var b As Boolean
> ' <— Set d as your code requires
> b = d.Invoke(1, 10, "An example string") ' Values are indicative
> ```
>
> <div class="important">
>
> <div class="title">
>
> Important
>
> </div>
>
> This method does not support optional parameters.
>
> </div>

## Notes

A <span class="title-ref">Delegate</span> is a function pointer with a method signature. It has a single method, “Invoke” whose parameters and return value match the <span class="title-ref">Delegate</span>'s parameters and return type. The Invoke method calls the method the <span class="title-ref">Delegate</span> instance represents.

<div class="note">

<div class="title">

Note

</div>

While <span class="title-ref">delegates</span> are objects, you cannot create a subclass of a <span class="title-ref">Delegate</span> type.

</div>

Delegates decouple interface from implementation in a similar way to events or interfaces. This decoupling allows you to treat a method implementation as a variable that is changeable based on runtime conditions. They represent methods that are callable without knowledge of the target object. You can change the function the <span class="title-ref">Delegate</span> points to on the fly.

A <span class="title-ref">Delegate</span> can be declared in either a module or a class. You use the Project \> Add \> <span class="title-ref">Delegate</span> menu command or the (optional) Add <span class="title-ref">Delegate</span> button in the Code Editor to create a <span class="title-ref">Delegate</span> entry.

A <span class="title-ref">Delegate</span> must have a name and can have optional parameters and a return type.

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

### Creating a <span class="title-ref">delegate</span> value

Once you have added the <span class="title-ref">Delegate</span> as described above, create a variable or property of your <span class="title-ref">Delegate</span> type. For example, if you named your <span class="title-ref">Delegate</span> *MyDelegate*, you would declare a variable or property with *myProp As MyDelegate*.

There are two ways to create values that can be stored in \`delegates\`:

Usually, `AddressOf</api/language/addressof>` (and `WeakAddressOf</api/language/weakaddressof>`) are used, taking an existing method and returning a function pointer in form of a <span class="title-ref">Delegate</span>.

The other way is to specify a function pointer address of type Ptr, passing it to a <span class="title-ref">Delegate</span> constructor. Assuming that there's a <span class="title-ref">Delegate</span> declared as *Sub SimpleProc()*, this could work as follows:

``` xojo
Var pp As Ptr = AnOSFunctionThatReturnsAFunctionPointer()
Var sp As New SimpleProc(pp)
sp.Invoke()
```

## Sample code

Suppose you've added a <span class="title-ref">Delegate</span> named *MethodCaller* with no parameters or return type, and there is a checkbox *MethodCheck* that will determine which method to use.

``` xojo
Var callMethod As MethodCaller
If MethodCheck.Value Then
  callMethod = AddressOf TestMethod
Else
  callMethod = AddressOf AnotherMethod
End If
callMethod.Invoke
```

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

If you've defined your <span class="title-ref">Delegate</span> with parameters and a return type, the code might look like this instead:

``` xojo
someValue = callMethod.Invoke(param1, param2)
```

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

See the `Invoke<delegate.invoke>` method for more examples.

A <span class="title-ref">Delegate</span> is a good way to allow one object to send the same message to various other objects. You can define a *Register* method that takes the <span class="title-ref">Delegate</span> as a parameter and adds it to an array of your <span class="title-ref">Delegate</span> type.

``` xojo
Sub Register (d As MessageDelegate)
  MessageDelegateArray.Add(d)
End Sub
```

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

When you need to send a message, you can call each <span class="title-ref">Delegate</span> in the array in a loop.

``` xojo
Sub SendMessage (msg As String)
  For Each d As MessageDelegate In MessageDelegateArray
    If d <> Nil Then
      d.Invoke(msg)
    End If
  Next d
End Sub
```

## Compatibility

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

<div class="seealso">

`Var</api/language/var>`, `Static</api/language/static>`, `Declare</api/language/declare>` statements; `-</api/language/operators/mathematical/->`, `+</api/language/operators/mathematical/+>`, `*</api/language/operators/mathematical/x>`, `/</api/language/operators/mathematical/division>`, `<</api/language/operators/comparison/less_than>`, `\<=</api/language/operators/comparison/less_than_or_equal>`, `=</api/language/operators/comparison/equals_operator>`, `\>=</api/language/operators/comparison/greater_than_or_equal>`, `\></api/language/operators/comparison/greater_than>`, `<></api/language/operators/comparison/not_equal>` `VarType</api/language/vartype>` functions; `AddHandler</api/language/addhandler>`, `Boolean</api/data_types/boolean>`, `Byte</api/data_types/additional_types/byte>`, `CFStringRef</api/data_types/additional_types/cfstringref>`, `Color</api/data_types/color>`, `CString</api/data_types/additional_types/cstring>`, `Currency</api/data_types/currency>`, `Double</api/data_types/double>`, `Int16</api/data_types/additional_types/int16>`, `Int32</api/data_types/additional_types/int32>`, `Int64</api/data_types/additional_types/int64>`, `Int8</api/data_types/additional_types/int8>`, `Integer</api/data_types/integer>`, `OSType</api/data_types/additional_types/ostype>`, `PString</api/data_types/additional_types/pstring>`, `Ptr</api/data_types/additional_types/ptr>`, `Int16</api/data_types/additional_types/int16>`, `Single</api/data_types/single>`, `String</api/data_types/string>`, `UInt16</api/data_types/additional_types/uint16>`, `UInt32</api/data_types/additional_types/uint32>`, `UInt64</api/data_types/additional_types/uint64>`, `UInt8</api/data_types/additional_types/uint8>`, `Variant</api/data_types/variant>`, `DesktopWindow.Handle<desktopwindow.handle>`, `WString</api/data_types/additional_types/wstring>` data types. `IsNumeric</api/language/isnumeric>`, `Mod</api/language/operators/mathematical/mod>`, `Str</api/text/str>`, `Val</api/text/val>`, `Vartype</api/language/vartype>`, functions.

</div>
