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
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor