Tangible Software Solutions

The Most Trusted Name in Source Code Conversion

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

Native C++:

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

C++/CLI:

property SomeReturnType ^default[int]
{
    SomeReturnType ^get(int i)
    {
        return SomeField[i];
    }
    void set(int i, SomeReturnType ^value)
    {
        SomeField[i] = value;
    }
}

Copyright © 1997 - 2017 Tangible Software Solutions Inc.