The Visual Basic .NET Collection Class


 
A collection is a way of grouping a set of related items. Many different types of collections exist. Collections are used in Visual Basic to keep track of many things, such as all the controls on a form (the Controls collection for example), and users can create their own collections to organize and manipulate objects.

Collections are a good way to keep track of objects that your application might need to dynamically create and destroy. The following code fragment shows how you might use the Add method of a collection object to keep a list of Widget objects the user has created.

' Declares and instantiates the Collection object.
Public widgetCollection As New Collection
' Creates a new Widget and adds it to the widgetCollection.
Private Sub MakeAWidget()
   Dim tempWidget As New Widget()
   widgetCollection.Add(tempWidget)
End Sub

The widgetCollection collection here organizes and exposes all of the Widget objects created through the MakeAWidget method. You can retrieve object references to each widget through the index of the collection. The size of the collection is adjusted automatically as each new Widget object is added. You can use the For Each...Next statements to iterate through the collection. If you want to give the Widget object a key by which it can be retrieved, you can supply a text string as the second parameter of the Add method, as described later in this section.

The New keyword in the declaration for the variable widgetCollection causes a Collection object to be created the first time the variable is referred to in code. Because Collection is a class, rather than a data type, you must create an instance of it and keep a reference to that instance (object) in a variable.

Like any other object, a Collection object is marked for garbage collection when the last variable that contains a reference to it is set to Nothing or goes out of scope; all the object references it contains are released when collected. For this reason, the variable widgetCollection is declared in the parent class, so that it exists throughout the life of the program.

Inside a Collection Object

A Collection object stores each item in an Object; thus the list of things you can add to a Collection object is the same as the list of things that can be stored in an object variable. This includes standard data types, objects, and arrays, as well as user-defined structures and class instances.

Because the Collection object stores each item as an object, Object is the type of reference the Item property returns. This presents the issue of how to access collection members later. If Option Strict is Off, you can implicitly convert a reference from the Collection to the appropriate type, as shown in this example.

Option Strict Off
Dim myString As String = "This is my String"
Dim aString As string
Dim myCollection As New Collection()
myCollection.Add(myString)
aString = myCollection.Item(1)   ' Collection item converted to string.

If Option Strict is On, however, you need to cast from Object to the type the collection contains. To obtain a reference from Item in this way, you can use the CType function to explicitly perform the conversion, as shown below:

Option Strict On
Dim myString As String = "This is my String"
Dim aString As String
Dim myCollection As New Collection()
myCollection.Add(myString)
aString = CType(myCollection.Item(1), String)

An alternative is to create your own strongly-typed collection. For a discussion on how to do this, see Walkthrough: Creating Your Own Collection Class.