Define Indexer for your own class : Indexer « Class Module « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Class Module » Indexer 
6.27.1.Define Indexer for your own class
Option Strict On
 Imports System

 Public Class MyItemList
     Private strings(255As String
     Private ctr As Integer = 0

     Public Sub New(ByVal ParamArray initialStrings( ) As String)
         Dim As String

         For Each s In initialStrings
             strings(ctr= s
             ctr += 1
         Next
     End Sub

     Public Sub Add(ByVal theString As String)
         If ctr >= Strings.Length Then
         Else
             Strings(ctr= theString
             ctr += 1
         End If
     End Sub

     Default Public Property Item(ByVal index As IntegerAs String
         Get
             If index < Or index >= strings.Length Then
             Else
                 Return strings(index)
             End If
         End Get
         Set(ByVal Value As String)
             If index >= ctr Then
             Else
                 strings(index= Value
             End If
         End Set
     End Property

     Public Function Count( ) As Integer
         Return ctr
     End Function
 End Class

 Public Class Tester
     Public Shared Sub Main( )
         Dim lbt As New MyItemList("Hello""World")
         Dim As Integer

         Console.WriteLine("After creation...")
         For i = To lbt.Count - 1
             Console.WriteLine("lbt({0}): {1}", i, lbt(i))
         Next

         lbt.Add("W")
         lbt.Add("I")
         lbt.Add("J")
         lbt.Add("t")

         Console.WriteLine("After adding strings...")
         For i = To lbt.Count - 1
             Console.WriteLine("lbt({0}): {1}", i, lbt(i))
         Next

         Dim subst As String = "e"
         lbt(1= subst

         Console.WriteLine("After editing strings...")
         For i = To lbt.Count - 1
             Console.WriteLine("lbt({0}): {1}", i, lbt(i))
         Next
     End Sub
 End Class
After creation...
lbt(0): Hello
lbt(1): World
After adding strings...
lbt(0): Hello
lbt(1): World
lbt(2): W
lbt(3): I
lbt(4): J
lbt(5): t
After editing strings...
lbt(0): Hello
lbt(1): e
lbt(2): W
lbt(3): I
lbt(4): J
lbt(5): t
6.27.Indexer
6.27.1.Define Indexer for your own class
6.27.2.Searchable Indexer
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.