15.3 The Collection ClassVisual Basic .NET offers a generic collection class named, aptly, Collection. In many ways, the Collection object serves as an object-oriented alternative to Array, much as ArrayList does. These two constructs (ArrayList and Collection) are very similar. Both offer and Add( ) and Remove( ) methods as well as an Item property. The Collection class, however, overloads the Item property to take a string as a key into the collection. This allows the Collection class to act as a dictionary, associating keys with values. You can also use the Item property to access members of the collection by index value; however the Collection uses a one-based index (i.e., the first element is index 1 rather than 0). Example 15-3 illustrates the use of a VB.NET Collection object. Example 15-3. Using a Collection objectOption Strict On Imports System Namespace CollectionDemo ' a class to hold in the array list Public Class Employee Private myEmpID As Integer Public Sub New(ByVal empID As Integer) Me.myEmpID = empID End Sub 'New Public Overrides Function ToString( ) As String Return myEmpID.ToString( ) End Function 'ToString Public Property EmpID( ) As Integer Get Return myEmpID End Get Set(ByVal Value As Integer) myEmpID = Value End Set End Property End Class 'Employee Class Tester Public Sub Run( ) Dim intCollection As New Collection( ) Dim empCollection As New Collection( ) Dim empCollection2 As New Collection( ) ' populate the Collections Dim i As Integer For i = 0 To 4 empCollection.Add(New Employee(i + 100)) intCollection.Add((i * 5)) Next i ' add key/value pairs empCollection2.Add(New Employee(1789), "George Washington") empCollection2.Add(New Employee(1797), "John Adams") empCollection2.Add(New Employee(1801), "Thomas Jefferson") ' print each member of the array For Each i In intCollection Console.Write("{0} ", i.ToString( )) Next i Console.WriteLine( ) Console.WriteLine("Employee collection...") Dim e As Employee For Each e In empCollection Console.Write("{0} ", e.ToString( )) Next e Console.WriteLine( ) Console.WriteLine("Employee collection 2...") For Each e In empCollection2 Console.Write("{0} ", e.ToString( )) Next e Console.WriteLine( ) ' retrieve an Employee by key Dim emp As Employee emp = empCollection2.Item("John Adams") Console.WriteLine( _ "Key John Adams retrieved empID {0}", emp.ToString( )) ' note that indexing is 1-based (rather than zero based) emp = empCollection2.Item(1) Console.WriteLine( _ "Index(1) retrieved empID {0}", emp.ToString( )) End Sub 'Run Shared Sub Main( ) Dim t As New Tester( ) t.Run( ) End Sub 'Main End Class 'Tester End Namespace 'CollectionDemo Output: 0 5 10 15 20 Employee collection... 100 101 102 103 104 Employee collection 2... 1789 1797 1801 Key John Adams retrieved empID 1797 Index(1) retrieved empID 1789 Example 15-3 creates three Collection objects (intCollection, empCollection, and empCollection2): Dim intCollection As New Collection( ) Dim empCollection As New Collection( ) Dim empCollection2 As New Collection( ) The first two objects are populated in For loops , just as the ArrayList was created in Example 15-2. Dim i As Integer For i = 0 To 4 empCollection.Add(New Employee(i + 100)) intCollection.Add((i * 5)) Next i The third Collection object, empCollection2, is populated using key values. Each new Employee is associated with a string, representing the name of the Employee: empCollection2.Add(New Employee(1789), "George Washington") empCollection2.Add(New Employee(1797), "John Adams") empCollection2.Add(New Employee(1801), "Thomas Jefferson") You retrieve objects from the collection much as you did from the ArrayLists: For Each i In intCollection Console.Write("{0} ", i.ToString( )) Next i Dim e As Employee For Each e In empCollection Console.Write("{0} ", e.ToString( )) Next e For Each e In empCollection2 Console.Write("{0} ", e.ToString( )) Next e You can, however, retrieve objects from the collection using either the key value or an index value (one-based): Dim emp As Employee emp = empCollection2.Item("John Adams") Console.WriteLine("Key John Adams retrieved empID {0}", emp.ToString( )) emp = empCollection2.Item(1) Console.WriteLine("Index(1) retrieved empID {0}", emp.ToString( )) |