Troubleshooting Collections

This page lists some common problems that can occur when working with collections.

Using the Wrong Type of Collection

There are several types of collections available to a Visual Basic developer: the Visual Basic Collection class and the collection classes furnished by the .NET Framework. These classes are not compatible with each other. This means that if you declare a variable to be of one type of collection, you cannot assign an object of another type to that variable. Also, you can access only the methods and properties of the collection type you have declared.

The main differences between the Visual Basic and .NET Framework collection classes include the following:

  • Index Base. .NET Framework collections are zero-based, while the Visual Basic collection is one-based. This means that the elements of a Visual Basic collection have index values from 1 through the value of the Count Property (Collection Object), whereas the elements of a .NET Framework collection have index values from 0 through one less than the value of the collection's Count property.

  • Element Type. The Visual Basic collection supports elements of type Object, which is not type-safe because you can add an element of any data type. This usually results in degraded performance because the compiler must box and unbox the elements to convert them to and from the Object Data Type. Some of the .NET Framework collections also have elements of type Object, but many others are strongly typed, meaning they support elements of a specific type, which makes them type-safe and usually results in optimal performance.

  • Keyed Elements. The Visual Basic collection allows you to specify a key when you add an element to it. The key is a unique String value which you can use later to access that particular element. The .NET Framework collections vary in regard to keys. Some support keys and some do not.

The namespaces that contain the various collection class definitions are the following:

  • Microsoft.VisualBasic — the Visual Basic Collection class

  • System.Collections — specific collection classes such as lists, queues, bit arrays, hash tables, and dictionaries

  • System.Collections.Generic — generic collection classes, which allow you to create strongly typed collections and specify the element data type when you create them

  • System.Collections.Specialized — specialized and strongly typed collection classes, such as linked-list and hybrid dictionaries, bit-vector and name-object collections, and string-only collections

Correct Approach

Determine which type of collection is most appropriate for your needs. Declare your collection variable to be of that type, and be sure to create an object of that same type. Use full qualification to ensure that you are specifying the collection type you intend to. The following example shows two declarations with full qualification.

Dim customers As New Microsoft.VisualBasic.Collection()
Dim stringQueue As New System.Collections.Generic.Queue(Of String)

Once you have created a collection of a specific type, be sure you use only the methods and properties defined on that type. Set Option Strict On to catch any incorrect object assignments or member accesses at compile time.

See Also

Concepts

Collections in Visual Basic

Visual Basic Collection Class

Reference

Option Strict Statement