Method

Operator_Subscript


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.

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

Operator_subscript(index as Integer) As Integer

The following syntax would then be valid:

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

Usage

Var variableName(ParamArray parameterName As Variant) As Variant

Tip

You can use the Assigns keyword as well allowing you to assign values just as you can with arrays.

Sample code

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

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

  Function Count() As Integer
    Return mArray.LastRowIndex + 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

All project types on all supported operating systems.

See also

Arrays.ResizeTo statements.