Use Indexer in VB.NET

This example shows the INDEXER in VB.NET Programming.
  • 2659
 

This article shows the use of the indexer in VB.NET.

Special property without any name or anonymous property. It allows to access some data from an object using an index number with the object where the object is not an array.

Item is the default property of an ArrayList which gets the object stored in the specified index. You can have only one Default property/Indexer per class. The Indexer can be overloaded.

Default properties should accept parameters

For example:

Class Employee

        Private mobiles As String()

        Public Sub SetMobiles(ByVal m1 As String, ByVal m2 As String, ByVal m3 As String)

            mobiles = New String(2) {}

            mobiles(0) = m1

            mobiles(1) = m2

            mobiles(2) = m3

        End Sub

        Default Public ReadOnly Property Item(ByVal index As Integer) As String        

                                                                                'Indexer implementation

             Get

                Return mobiles(index - 1)

            End Get

        End Property

        Public Shared Sub Main(ByVal args As String())

            Dim e As New Employee()

            e.SetMobiles("921233444", "947878787", "9367676")

            Console.WriteLine("Third mobile is {0}", e(1))

        End Sub

    End Class

End Module

OUTPUT:

 indexer.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.