Keyword

# Extends

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

## Description

Used in a module method declaration to indicate that the method is to be called using the dot operator ("."), as if it were an object method of the first parameter.

## Usage

``` xojo
Extends parameter As DataType
```

| Part        | Type                    | Description                                                                                                                                                                                                                  |
|-------------|-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| *parameter* | same type as *dataType* | The name of the first parameter. If *parameter* is `Nil</api/language/nil>`, the <span class="title-ref">Extends</span> method is not executed, and a `NilObjectException</api/exceptions/nilobjectexception>` is generated. |
| *DataType*  | data type or class      | The data type for which the method can be called.                                                                                                                                                                            |

## Notes

The <span class="title-ref">Extends</span> keyword allows you to call a user-defined method as if it were part of a class. You use the <span class="title-ref">Extends</span> keyword only for the first parameter in the method declaration. <span class="title-ref">Extends</span> indicates that this parameter is to be used on the left side of the dot operator. The remaining parameters in the declaration, if any, are used normally.

Extension methods can be used with classes or other data types. For example, you can define methods that extend `String</api/data_types/string>`, `Color</api/data_types/color>`, and `Integer data types</api/data_types/additional_types/information_about_additional_integer_data_types>`. You can also extend arrays.

Methods declared in this way are sometimes called "class extension methods" even though they are not actually part of a class. You can use <span class="title-ref">Extends</span> only for methods in a module. <span class="title-ref">Extends</span> cannot be used to override another method. Extension methods are not virtual, since they are not part of a class.

Extension methods are an example of "syntactic sugar". There is no difference in capability between an extension method and a normal module method; each specifies a particular calling syntax. (Many built-in methods are provided in both versions, so that either calling syntax will work.) Extension methods can make calling code more regular and readable, and if used consistently, can make it easier to remember which syntax to use. On the other hand, extension methods can make the dependency of a class on a module less obvious to a programmer.

## Sample code

<span class="title-ref">Extends</span> can allow more readable and concise calling code. This example <span class="title-ref">Extends</span> the `String</api/data_types/string>` data type with a function that determines whether the string is a palindrome (reads the same forwards and backwards). In a module, create this function:

``` xojo
Function IsPalindrome(Extends source As String) As Boolean
  If source.IsEmpty Then Return False

  Var cleaned As String = source.ReplaceAll(" ", "").Lowercase
  Var reversed As String

  For i As Integer = cleaned.Length - 1 DownTo 0
    reversed = reversed + cleaned.Middle(i, 1)
  Next

  Return cleaned = reversed
End Function
```

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

The <span class="title-ref">Extends</span> keyword indicates that the first parameter does not appear in the parameter list of the actual method call. Instead, it is used on the left side of the dot operator. The remaining parameter is used normally:

``` xojo
Var s1 As String = "Level"
Var s2 As String = "Hello"

MessageBox(s1.IsPalindrome.ToString) ' True
MessageBox(s2.IsPalindrome.ToString) ' False
```

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

Without <span class="title-ref">Extends</span>, the method call would be functionally equivalent:

``` xojo
Var s1 As String = "Level"

If IsPalindrome(s1) Then
  MessageBox(s1 + " is a palindrome") ' True
Else
  MessageBox(s1 + " is not a palindrome") ' False
End If
```

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

Here's another example: controls and windows have built-in Left and Top properties, but not Bottom and Right properties. You can simulate these using \`Extends\`:

``` xojo
Function Right(Extends ctl As DesktopUIControl) As Integer
  Return ctl.Left + ctl.Width
End Function

Function Bottom(Extends ctl As DesktopUIControl) As Integer
  Return ctl.Top + ctl.Height
End Function
```

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

As with the previous example, the <span class="title-ref">Extends</span> keyword indicates that the first parameter is used before the dot operator. Since there are no other parameters, calls to these methods don't use a parameter list. Here's how you might use these functions to place a button below a text field, and to align it with the text field on the right:

``` xojo
MyButton.Top = MyTextField.Bottom + 10
MyButton.Left = MyTextField.Right - MyButton.Width
```

## Compatibility

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

<div class="seealso">

`Extension Methods</getting_started/using_the_xojo_language/modules/extension_methods>` topic, `Extends can only be used on the first parameter</api/errors>` Error.

</div>
