Method

# Operator_Subscript

<div class="rst-class">

forsearch

</div>

Operator

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

## Description

Allows a class to make an array-like syntax available where you simply pass parameters without calling a method.

To use it, create a method named `Operator_Subscript` in your class and implement it.

<div class="important">

<div class="title">

Important

</div>

This is not currently supported for Android.

</div>

For example, say a class named *foo* implemented this method as:

Operator_subscript(index as `Integer</api/data_types/integer>`) As `Integer</api/data_types/integer>`

The following syntax would then be valid:

``` xojo
Var f as New foo
MessageBox(f(12).ToString)
```

## Usage

Var variableName(`ParamArray</api/language/paramarray>` parameterName As `Variant</api/data_types/variant>`) As `Variant</api/data_types/variant>`

<div class="tip">

<div class="title">

Tip

</div>

You can use the `Assigns</api/language/assigns>` keyword as well allowing you to assign values just as you can with arrays.

</div>

## Sample code

This example class uses Operator_subscript among other methods to create an array that is 1-based rather than 0-based.

``` xojo
Class OneBasedArray
  Sub Constructor(bounds As Integer = 0)
    mArray.ResizeTo(bounds - 1)
  End Sub

  Function Count() As Integer
    Return mArray.LastIndex + 1
  End Function

  Sub Operator_ResizeTo(newSize As Integer)
    mArray.ResizeTo(newSize - 1)
  End Sub

  Function Operator_Subscript(index As Integer) As Variant
    Return mArray(index - 1)
  End Function

  Sub Operator_Subscript(index As Integer, Assigns v As Variant)
    mArray(index - 1) = v
  End Sub

  Private mArray() As Variant
End Class
```

## Compatibility

|                       |                               |
|-----------------------|-------------------------------|
| **Project Types**     | Console, Desktop, Mobile, Web |
| **Operating Systems** | iOS, Linux, macOS, Windows    |

<div class="seealso">

`Arrays.ResizeTo<arrays.resizeto>` statements.

</div>
