The Visual Basic collection classes basically hold items and don’t provide a lot of extra functionality. Other classes described later in this chapter provide more features.
The following sections describe the simple collection classes in Visual Basic: ArrayList, StringCollection, and NameValueCollection. They also describe strongly typed collections that you can build to make code that uses these classes a bit easier to debug and maintain.
The ArrayList class is a resizable array. You can add and remove items from any position in the list and it resizes itself accordingly. The following table describes some of the class’s more useful properties and methods.
PROPERTY/METHOD | PURPOSE |
Add | Adds an item at the end of the list. |
AddRange | Adds the items in an object implementing the ICollection interface to the end of the list. |
BinarySearch | Returns the index of an item in the list. The items must implement the IComparable interface, or you must provide the Sort method with an IComparer object. |
Capacity | Gets or sets the number of items that the list can hold. |
Clear | Removes all of the items from the list. |
Contains | Returns True if a specified item is in the list. |
CopyTo | Copies some of the list or the entire list into a one-dimensional Array object. |
Count | Returns the number of items currently in the list. This is always less than or equal to Capacity. |
GetRange | Returns an ArrayList containing the items in part of the list. |
IndexOf | Returns the zero-based index of the first occurrence of a specified item in the list. |
Insert | Adds an item at a particular position in the list. |
InsertRange | Adds the items in an object implementing the ICollection interface to a particular position in the list. |
Item | Returns the item at a particular position in the list. |
LastIndexOf | Returns the zero-based index of the last occurrence of a specified item in the list. |
Remove | Removes the first occurrence of a specified item from the list. |
RemoveAt | Removes the item at the specified position in the list. |
RemoveRange | Removes the items in the specified positions from the list. |
Reverse | Reverses the order of the items in the list. |
SetRange | Replaces the items in part of the list with new items taken from an ICollection object. |
Sort | Sorts the items in the list. The items must implement the IComparable interface, or you must provide the Sort method with an IComparer object. |
ToArray | Copies the list’s items into a one-dimensional array. The array can be an array of objects, an array of a specific type, or an Array object (holding objects). |
TrimToSize | Reduces the list’s allocated space so that it is just big enough to hold its items. This sets Capacity = Count. |
A single ArrayList object can hold objects of many different kinds. The following code creates an ArrayList and adds a string, Form object, integer, and Bitmap to it. It then loops through the items in the list and displays their types.
Dim array_list As New ArrayList
array_list.Add("What?")
array_list.Add(Me)
array_list.Add(1001)
array_list.Add(New Bitmap(10, 10))
For Each obj As Object In array_list
Debug.WriteLine(obj.GetType.ToString)
Next obj
The following text shows the results:
System.String
UseArrayList.Form1
System.Int32
System.Drawing.Bitmap
The value displayed for the second item depends on the name of the project (in this case, UseArrayList).
A StringCollection is similar to an ArrayList, except that it can hold only strings. Because it works only with strings, this class provides some extra type checking that the ArrayList does not. For example, if your program tries to add an Employee object to a StringCollection, the collection raises an error.
To take advantage of this extra error checking, you should always use a StringCollection instead of an ArrayList if you are working with strings. Of course, if you need other features (such as the fast lookups provided by a Hashtable), you should use one of the classes described in the following sections.
The NameValueCollection class is a collection that can hold more than one string value for a particular key (name). For example, you might use employee names as keys. The string values associated with a particular key could include extension, job title, employee ID, and so forth. Of course, you could also store the same information by putting extension, job title, employee ID, and the other fields in an object or structure, and then storing the objects or structures in some sort of collection class such as an ArrayList. A NameValueCollection, however, is useful if you don’t know ahead of time how many strings will be associated with each key.
The following table describes some of the NameValueCollection’s most useful properties and methods.
PROPERTY/METHOD | DESCRIPTION |
Add | Adds a new name/value pair to the collection. If the collection already holds an entry for the name, it adds the new value to that name’s values. |
AllKeys | Returns a string array holding all of the key values. |
Clear | Removes all names and values from the collection. |
CopyTo | Copies items starting at a particular index into a one-dimensional Array object. This copies only the items (see the Item property), not the keys. |
Count | Returns the number of key/value pairs in the collection. |
Get | Gets the items for a particular index or name as a comma-separated list of values. |
GetKey | Returns the key for a specific index. |
GetValues | Returns a string array containing the values for a specific name or index. |
HasKeys | Returns True if the collection contains any non-null keys. |
Item | Gets or sets the item for a particular index or name as a comma-separated list of values. |
Keys | Returns a collection containing the keys. |
Remove | Removes a particular name and all of its values. |
Set | Sets the item for a particular index or name as a comma-separated list of values. |
Note that there is no easy way to remove a particular value from a name. For example, if a person’s name is associated with extension, job title, and employee ID, it is not easy to remove only the job title.
Example program UseNameValueCollection, which is available for download on the book’s website, demonstrates NameValueCollection class features.