ARTICLE

How to use INDEXER in VB.NET

Posted by Rohatash Kumar Articles | Visual Basic 2010 August 31, 2010
Tags: INDEXER, VB.NET
This example shows the INDEXER in VB.NET Programming.
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor