Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Visual Basic Language » Indexers in VB.NET

Indexers in VB.NET

In this article I will explain you about indexer in VB.NET.

Author Rank :
Page Views : 2015
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


HTML clipboard

Indexers, another nifty feature of VB.NET, are similar to the overloaded [] (array subscript) operator in C++. An indexer allows you to access a class instance in terms of a member array.

An indexer declaration may include a set of attributes; a new modifier; a valid combination of the public, private, protected, and internal access modifiers; and one of the virtual, override, or abstract modifiers.

The type of an indexer declaration specifies the element type of the indexer introduced by the declaration. Unless the indexer is an explicit interface member implementation, the type is followed by the keyword this. For an explicit interface member implementation, the type is followed by an interface type, a period (.), and the keyword this. Unlike other members, indexers do not have user defined names.

The formal parameter list of an indexer corresponds to that of a method, with two differences: at least one parameter must be specified, and the ref and out parameter modifiers are not permitted.

The accessors specify the executable statements associated with reading and writing indexer elements.

Even though the syntax for accessing an indexer element is the same as that for an array element, an indexer element is not classified as a variable. Thus, it is not possible to pass an indexer element as a ref or out parameter.

It is an error for an indexer accessor to declare a local variable with the same name as an indexer parameter. With these differences in mind, all rules defined in apply to indexer accessors as well as property accessors.

Indexers and properties, although very similar in concept, differ in the following ways:

  • A property is identified by its name whereas an indexer is identified by its signature.

  • A property is accessed through a simple-name or a member-access whereas an indexer element is accessed through an element-access.

  • A property can be a static member whereas an indexer is always an instance member.

  • A get accessor of a property corresponds to a method with no parameters whereas a get accessor of an indexer corresponds to a method with the same formal parameter list as the indexer.

  • A set accessor of a property corresponds to a method with a single parameter named value whereas a set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value.

To understand these concepts, refer to the code in Listing 5.56.

Listing 5.56: Indexer Example


Imports System
Imports System.Net
Class GetDNSaliases
    Private m_arrAlias As String()
    Public Sub Fetch(ByVal strHost As String)
        Dim iphe As IPHostEntry = Dns.GetHostByAddress(strHost)
        m_arrAlias = iphe.Aliases
    End Sub
    Default Public ReadOnly Property Item(ByVal nIndex As Integer) As String
        'indexer
        Get
            Return m_arrAlias(nIndex)
        End Get
    End Property
    Public ReadOnly Property Count() As Integer
        ' property
        Get
            Return m_arrAlias.GetUpperBound(0)
        End Get
    End
Property
End Class
Class MyApp
    Public Shared str1 As String
    Public Shared Sub Main()
        Dim myGetDNSaliases As New GetDNSaliases()
        Do
            Try
                Console.WriteLine("write a valid IP Address and press ENTER (to exit enter ""q"").")
                If (str1 = Console.ReadLine()) = "q" Then
                    Exit Try
                End If
                myGetDNSaliases.Fetch(str1)
                Dim nCount As Int32 = myGetDNSaliases.Count
                Console.WriteLine("Found {1} aliases for IP {0}", str1, nCount)
                Dim i As Int32 = 0
                While i < nCount
                    Console.WriteLine(myGetDNSaliases(i))
                    System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
                End While
            Catch e As Exception
                Console.WriteLine("Exception occurred!{0}" & vbCr & vbLf, e.ToString())
            Finally
                'final code to be executed
            End Try
        Loop While True
        Console.ReadLine()
    End
Sub
End Class

The code in this listing generates the display in Figure 5.16.

Figure 5.16: Screen Output Generated from Listing 5.56

indexer1.gif

Conclusion


Hope this article would have helped you in understanding indexer  in VB.NET.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Sapna
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
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.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.