Tangible Software Solutions

Indexer Methods in C#, VB, and C++

Most programming languages (except for Java) have syntax which allows indexing an object reference as if the object were a proper array. These are usually called 'indexer' methods.

C#:

SomeReturnType this[int i]
{
    get
    {
        return SomeField[i];
    }
    set
    {
        SomeField[i] = value;
    }
}

VB:

Default Property Item(ByVal i As Integer) As SomeReturnType
    Get
        Return SomeField(i)
    End Get
    Set(ByVal value As SomeReturnType)
        SomeField(i) = value
    End Set
End Property

C++:

SomeReturnType &operator[] (int i)
{
    return SomeField[i];
}

Copyright © 2004 – 2024 Tangible Software Solutions Inc.