The Collection Class |
|
Like an array, a collection is a series of items of the same type. The problem with an array is that you must know in advance the number of items that will make up the list. There are cases you don't know, you can't know, or you can't predict the number of items of the list. The solution is to create your own list. A list is a a series in which you don't specify the maximum number of items but you allow the user of the list to add, locate, or remove items at will. Instead creating your own list class as we did above, the Microsoft Visual Basic programming language, along with the .NET Framework provides various classes that can be used to create different types of lists. Most of the classes are easy to use once you get used to them. The idea is to know what classes are available, what a class does, and what class to use for a particular assignment.
To provide an easy means of creating a collection, the Microsoft Visual Basic programming language has its own class called Collection. This class is particularly easy and it is highly useful. To use it, first declare its variable and initialize it using the New operator. Here is an example: Module Exercise Sub Main() Dim coll As Collection = New Collection End Sub End Module Although not the most equipped list class, it provides all of the basic functionality you need to create a list, add items to it, and retrieve item when necessary.
The primary operation performed on a list consists of populating it with one or more items. To support this, the Collection class is equipped with the Add() method. Its syntax is: Public Sub Add( _ ByVal Item As Object, _ Optional ByVal Key As String, _ Optional ByVal { Before | After } As Object = Nothing) The first argument is the only one required. It specifies the item to be added to the list. Here is an example of calling it various times to add a few strings to a collection: Module Exercise Sub Main() Dim coll As Collection = New Collection coll.Add("Jeremy") coll.Add("Fallen") coll.Add("Patricia") coll.Add("Payne") End Sub End Module The item you want to add to the collection doesn't have to be of a regular data type. As its data type indicates, the first argument of the Add() method can be an .NET Framework class, whether you created your own or not. For example, imagine you created a class called Triangle as follows: Public Class Triangle Private _bse As Double Private _hgt As Double Public Sub New() _bse = 0 _hgt = 0 End Sub Public Sub New(ByVal bse As Double, ByVal hgt As Double) _bse = bse _hgt = hgt End Sub Public Property Base() As Double Get Return _bse End Get Set(ByVal Value As Double) If _bse < 0 Then _bse = 0 Else _bse = Value End If End Set End Property Public Property Height() As Double Get Return _hgt End Get Set(ByVal Value As Double) If _hgt < 0 Then _hgt = 0 Else _hgt = Value End If End Set End Property Public ReadOnly Property Area() As Double Get Return _bse * _hgt / 2 End Get End Property End Class Because this class is implicitly derived from Object, you can create its objects and add them to the collection by passing them to Add() method of your Collection instance. Here is an example: Module Exercise Public Class Triangle Private _bse As Double Private _hgt As Double Public Sub New() _bse = 0 _hgt = 0 End Sub Public Sub New(ByVal bse As Double, ByVal hgt As Double) _bse = bse _hgt = hgt End Sub Public Property Base() As Double Get Return _bse End Get Set(ByVal Value As Double) If _bse < 0 Then _bse = 0 Else _bse = Value End If End Set End Property Public Property Height() As Double Get Return _hgt End Get Set(ByVal Value As Double) If _hgt < 0 Then _hgt = 0 Else _hgt = Value End If End Set End Property Public ReadOnly Property Area() As Double Get Return _bse * _hgt / 2 End Get End Property End Class Sub Main() Dim colTriangle As Collection = New Collection colTriangle.Add(New Triangle(38.64, 22.18)) colTriangle.Add(New Triangle(15.28, 40.22)) colTriangle.Add(New Triangle(9.05, 24.15)) colTriangle.Add(New Triangle(104.31, 36.22)) colTriangle.Add(New Triangle(25.16, 5.52)) End Sub End Module |
|
Item Count Collection |
While adding the items in the collection or after doing this, you may want to know the number of items in the collection. To get this information, the Collection class provides the Count property, which returns an integer. |
Item Retrieval |
Once a collection contains some items, you can access them. To support this, the Collection class is equipped with the Item() property. Each item in the collection occupies an index. Unlike an array, a Collection object is 1-based. This means that the first item as an index of 1. The second has an index of 2, and so on. To access an item, pass its index to the parentheses of the Item property. Here is an example: Module Exercise Sub Main() Dim coll As Collection = New Collection coll.Add("Jeremy") coll.Add("Fallen") coll.Add("Patricia") coll.Add("Payne") For i As Integer = 1 To 4 Step 1 Console.WriteLine(coll(i)) Next End Sub End Module This would produce: Jeremy Fallen Patricia Payne In the same way, if you have based your items from a class, you can retrieve each using its index in the collection. Here is an example: Module Exercise Public Class Triangle Private _bse As Double Private _hgt As Double Public Sub New() _bse = 0 _hgt = 0 End Sub Public Sub New(ByVal bse As Double, ByVal hgt As Double) _bse = bse _hgt = hgt End Sub Public Property Base() As Double Get Return _bse End Get Set(ByVal Value As Double) If _bse < 0 Then _bse = 0 Else _bse = Value End If End Set End Property Public Property Height() As Double Get Return _hgt End Get Set(ByVal Value As Double) If _hgt < 0 Then _hgt = 0 Else _hgt = Value End If End Set End Property Public ReadOnly Property Area() As Double Get Return _bse * _hgt / 2 End Get End Property End Class Sub Main() Dim colTriangle As Collection = New Collection colTriangle.Add(New Triangle(38.64, 22.18)) colTriangle.Add(New Triangle(15.28, 40.22)) colTriangle.Add(New Triangle(9.05, 24.15)) colTriangle.Add(New Triangle(104.31, 36.22)) colTriangle.Add(New Triangle(25.16, 5.52)) For i As Integer = 1 To 5 Step 1 Dim tngl As Triangle = colTriangle(i) Console.WriteLine("%%%% Triangles %%%%") Console.WriteLine("Height: {0}", tngl.Height) Console.WriteLine("Base: {0}", tngl.Base) Console.WriteLine("Area: {0}", tngl.Area) Next End Sub End Module This would produce: %%%% Triangles %%%% Height: 22.18 Base: 38.64 Area: 428.5176 %%%% Triangles %%%% Height: 40.22 Base: 15.28 Area: 307.2808 %%%% Triangles %%%% Height: 24.15 Base: 9.05 Area: 109.27875 %%%% Triangles %%%% Height: 36.22 Base: 104.31 Area: 1889.0541 %%%% Triangles %%%% Height: 5.52 Base: 25.16 Area: 69.4416 Of course, as a collection, a list you create using the Collection class can also be navigated through using the For Each operator. Here is an example: Module Exercise Public Class Triangle . . . No Change End Class Sub Main() Dim colTriangle As Collection = New Collection colTriangle.Add(New Triangle(38.64, 22.18)) colTriangle.Add(New Triangle(15.28, 40.22)) colTriangle.Add(New Triangle(9.05, 24.15)) colTriangle.Add(New Triangle(104.31, 36.22)) colTriangle.Add(New Triangle(25.16, 5.52)) For Each Angle3 As Triangle In colTriangle Console.WriteLine("%%%% Triangles %%%%") Console.WriteLine("Height: {0}", Angle3.Height) Console.WriteLine("Base: {0}", Angle3.Base) Console.WriteLine("Area: {0}", Angle3.Area) Next End Sub End Module
|
Item Deletion |
When using a list, if the presence of one of its items is not required, you can delete. To support this, the Collection class provides the Remove() method. Its syntax is: Public Overloads Sub Remove(ByVal { Key As String | Index As Integer }) This method takes as argument the index of the item to be deleted. You can pass the index as a string or as an integer. Here is an example of calling the Collection.Remove() method: Module Exercise Sub Main() Dim coll As Collection = New Collection coll.Add("Jeremy") coll.Add("Fallen") coll.Add("Patricia") coll.Add("Payne") Console.WriteLine("--- First Call ---") For i As Integer = 1 To coll.Count Step 1 Console.WriteLine(coll(i)) Next Console.WriteLine() coll.Remove(2) Console.WriteLine("--- Second Call ---") For i As Integer = 1 To coll.Count Step 1 Console.WriteLine(coll(i)) Next Console.WriteLine() End Sub End Module This would produce: --- First Call --- Jeremy Fallen Patricia Payne --- Second Call --- Jeremy Patricia Payne |
|
||
Home | Copyright © 2005-2016, FunctionX | |
|