O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Collection Class

Namespace

Microsoft.VisualBasic

Createable

Yes

Syntax

Dim objectvariable As [New] Collection
objectvariable (required; 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 3.

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

Dim obj As New Collection

or:

Dim obj As Collection
obj = 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 entry). ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required