Use ArrayList to Store your own class : ArrayList « Data Structure « VB.Net






Use ArrayList to Store your own class

Use ArrayList to Store your own class
 
Imports System
Imports System.Collections
 
Public Class MainClass

    Shared Sub Main(ByVal args As String())
             Dim empArray As New ArrayList( )
             Dim intArray As New ArrayList( )

             ' populate the arraylists
             Dim i As Integer
             For i = 0 To 4
                 empArray.Add(New EnumerableClass("A", "B"))
                 intArray.Add((i * 5))
             Next i

             ' print each member of the array
             For Each i In intArray
                 Console.Write("{0} ", i.ToString( ))
             Next i

             Console.WriteLine(ControlChars.Lf)
             ' print each employee
             Dim e As EnumerableClass
             For Each e In empArray
                 Console.Write("{0} ", e.ToString( ))
             Next e

             Console.WriteLine(ControlChars.Lf)
             Console.WriteLine("empArray.Capacity: {0}", empArray.Capacity)

    End Sub

End Class

     Public Class EnumerableClass : Implements IEnumerable
         Private strings( ) As String
         Private ctr As Integer = 0

         Private Class MyEnumerator
             Implements IEnumerator
             Private currentListBox As EnumerableClass
             Private index As Integer

             Public Sub New(ByVal currentListBox As EnumerableClass)
                 Me.currentListBox = currentListBox
                 index = -1
             End Sub

             Public Function MoveNext( ) As Boolean _
               Implements IEnumerator.MoveNext
                 index += 1
                 If index >= currentListBox.strings.Length Then
                     Return False
                 Else
                     Return True
                 End If
             End Function

             Public Sub Reset( ) _
               Implements IEnumerator.Reset
                 index = -1
             End Sub

             Public ReadOnly Property Current( ) As Object _
             Implements IEnumerator.Current
                 Get
                     Return currentListBox(index)
                 End Get
             End Property
         End Class  ' end nested class

         Public Function GetEnumerator( ) As IEnumerator _
         Implements IEnumerable.GetEnumerator
             Return New MyEnumerator(Me)
         End Function

         Public Sub New( _
           ByVal ParamArray initialStrings( ) As String)
             ReDim strings(7)

             Dim s As String
             For Each s In initialStrings
                 strings(ctr) = s
                 ctr += 1
             Next
         End Sub

         Public Sub Add(ByVal theString As String)
             strings(ctr) = theString
             ctr += 1
         End Sub

         Default Public Property Item( _
           ByVal index As Integer) As String
             Get
                 If index < 0 Or index >= strings.Length Then
                     ' handle bad index
                     Exit Property
                 End If
                 Return strings(index)
             End Get
             Set(ByVal Value As String)
                 strings(index) = Value
             End Set
         End Property

         Public Function GetNumEntries( ) As Integer
             Return ctr
         End Function

     End Class


           
         
  








Related examples in the same category

1.Store Objects into ArrayListStore Objects into ArrayList
2.ArrayList: add, remove, IndexOf, Count, Contains, TrimToSize, GetEnumeratorArrayList: add, remove, IndexOf, Count, Contains, TrimToSize, GetEnumerator
3.Add element to an ArrayList and get its LengthAdd element to an ArrayList and get its Length
4.Convert ArrayList to Array
5.Store different Class in an ArrayListStore different Class in an ArrayList
6.ArrayList Class Implements the IList interface
7.Adds an object to the end of the ArrayList.
8.ArrayList.BinarySearch Searches the entire sorted ArrayList for an element using the default comparer
9.ArrayList.BinarySearch (Object, IComparer)
10.ArrayList.Clear removes all elements from the ArrayList.
11.ArrayList.CopyTo copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
12.ArrayList.FixedSize returns an ArrayList wrapper with a fixed size.
13.ArrayList.GetEnumerator returns an enumerator for a range of elements in the ArrayList.
14.ArrayList.GetRange returns an ArrayList which represents a subset of the elements in the source ArrayList.
15.ArrayList.IndexOf searches for the specified Object and returns the zero-based index
16.ArrayList.Insert inserts an element into the ArrayList at the specified index.
17.ArrayList.IsReadOnly Property tells whether the ArrayList is read-only.
18.ArrayList.IsSynchronized Property tells whether access to the ArrayList is synchronized (thread safe).
19.ArrayList.Item Property gets or sets the element at the specified index.
20.ArrayList.LastIndexOf searches for Object and returns the zero-based index of the last occurrence
21.ArrayList.Remove removes the first occurrence of a specific object from the ArrayList.
22.ArrayList.Repeat Method returns an ArrayList whose elements are copies of the specified value.
23.Reverses the order of the elements in the entire ArrayList.
24.Sorts the elements in the ArrayList using the specified comparer.
25.Sorts the elements in a range in ArrayList using the specified comparer.
26.Sorts the elements in the entire ArrayList.
27.Copies the elements of the ArrayList to a new array of the specified element type.
28.Reverses the order of the elements in a range.