Name

Collection Class

Namespace

Microsoft.VisualBasic

Createable

Yes

Syntax

Dimobjectvariable As [New] Collection
objectvariable

Use: Required

Data Type: Collection

The name of the Collection object

Description

A Collection object allows you to store members of any data type, including object data types or even other collection objects, and to retrieve them using a unique key.

Collection objects allow us to create a form of associative array, which is an array whose members are indexed by something more meaningful than an integer. The real power of a collection comes by using collections with class objects. The Collection object is discussed in more detail in Chapter 2.

Collection objects are created in exactly the same way as other objects, as in:

Dimobj As New Collection

or:

Dim obj As Collectionobj = New Collection

In the former syntax, the Collection object is created at the time that the obj variable is declared, which may be sooner than you actually need the Collection object. The latter syntax gives you more control over the creation process.

Rules at a Glance

  • You can use a Collection object to store data of any data type, including object types and even other Collection objects.

  • The Add method of the Collection object is used to add items to the collection (see the Collection.Add entry).

  • Members of a collection can be accessed using either their ordinal number or their key, assuming that one was assigned at the time that the member was added to the collection (see the Collection.Item ...

Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.