Share via


Collections in Visual Basic

In general terms, a collection is an object used for grouping and managing related objects. For example, every Form has a collection of controls. (You can access this collection through the form's Controls property.) This collection is an object that represents all the controls on that form. It allows you to retrieve a control in the collection by its index, and to loop through the elements of the collection using a For Each...Next Statement (Visual Basic).

However, there are several kinds of collections, and they differ from each other in several ways.

Different Kinds of Collections

Visual Basic also provides a Collection class, with which you can define and create your own collections. Like a form's Controls collection, the Collection class also provides you with the built-in functionality to loop through elements using For Each...Next and to retrieve elements by index. For more information, see Collection.

However, the two types of collections do not interoperate with each other. For example, the following code generates a compiler error.

Dim localControls As Collection

' The following line generates a COMPILER ERROR.

localControls = Me.Controls()

The collections are incompatible because the Controls collection is a .NET Framework collection, while the variable localControls is a Visual Basic Collection. The two kinds of collections are implemented from different classes. Their methods are similar but not identical, and their indexing schemes are different.

Zero-Based and One-Based Collections

A collection can be zero-based or one-based, depending on what its starting index is. The former means that the index of the first item in the collection is 0, and the latter means that it is 1. An example of a zero-based collection is the .NET Framework Controls collection, discussed earlier on this page. The Visual Basic Collection object is an example of a one-based collection.

One-based collections can be more intuitive to Visual Basic users, because the index ranges from 1 through the value of the Count property, which returns the number of items in a collection. The index of a zero-based collection, by contrast, ranges from 0 through one less than the value of the Count property. This can be appropriate when the index values are offsets from a base value or correspond to members of a zero-based enumeration.

.NET Framework collections are zero-based for the purpose of standardization. The Visual Basic Collection class is one-based for the purpose of compatibility with previous versions.

Index and Key Values

Instances of the Visual Basic Collection class allow you to access an item using either a numeric index or a String key. You can add items to Visual Basic Collection objects either with or without specifying a key. If you add an item without a key, you must use its numeric index to access it.

By contrast, collections such as System.Collections.ArrayList allow only a numeric index. You cannot associate keys with the elements of these collections, unless you construct your own mapping based, for example, on a String array holding the keys.

Adding and Removing Items

Collections also differ in whether or not you can add items to them, and if so, how those items are added. Because the Visual Basic Collection object is a general-purpose programming tool, it is more flexible than some other collections. It has an Add method for putting items into the collection and a Remove method for taking items out.

Certain specialized collections, on the other hand, do not allow you to add or remove elements using code. For example, the CheckedListBox.CheckedItems property returns a collection of references to items by index, but your code cannot add or remove items from the collection. Only the user can do this — by selecting or clearing the appropriate box in the user interface. Thus there is no Add or Remove method for this collection.

See Also

Tasks

How to: Create a Collection of Objects (Visual Basic)

How to: Create an Array of Objects (Visual Basic)

How to: Add, Delete, and Retrieve Items of a Collection (Visual Basic)

How to: Define Collections in Your Classes (Visual Basic)

How to: Iterate through a Collection in Visual Basic

Troubleshooting Collections (Visual Basic)

Concepts

Collection Initializers Overview (Visual Basic)

Managing Groups of Objects in Visual Basic

Visual Basic Collection Class

Managing Your Objects with Collections (Visual Basic)