Collection Class


Collection Class

Namespace

Microsoft.VisualBasic

Creatable

Yes

Syntax

     Dim result As [New] Collection 

Description

A Collection object allows you to store members of any data type, including mixed types, as a named group and to retrieve each one using a unique key.

Collection objects are a form of associative array, where each member is indexed by a meaningful and unique key. The Collection object is discussed in more detail in Chapter 4.

The following table lists some of the more useful and interesting members of the Collection class. Those marked with an asterisk (*) have separate entries in this chapter.

Member

Description

Add Method *

Adds a new item to the collection

Clear Method

Removes all items from the collection

Contains Method

Indicates whether the collection includes an item with a specific key

Count Property *

Returns the number of items currently found in the collection

Item Property *

Retrieves an item from the collection by position or by key

Remove Method *

Removes an item from the collection


Usage 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 first member in a collection is stored at ordinal position 1, not at 0, as is done with arrays.

  • A highly efficient method of enumerating the members of a collection is to use the ForEach...Next statement, as the following example shows:

         Dim unitedStates As New Collection     Dim scanState As String     ' ----- Build the list of states.     unitedStates.Add("Alabama", "AL")     unitedStates.Add("Alaska", "AK")     ...and so on...     ' ----- Process each state.     For Each scanState In unitedStates        MsgBox(scanState)   ' Displays full name of state     Next scanState 

  • The Collection class is implemented specifically within the Visual Basic portion of the .NET Framework. Other collection classes can be found in the System.Collections namespace.

Example

This example shows the basic use of the Collection class.

     Public Sub TestCollection(  )        Dim miscItems As New Collection        Dim displayText As string        ' ----- Add each item with a key, but with different types.        miscItems.Add("am", "second")  ' Adds a String        miscItems.Add(25, "third")     ' Adds an Integer        miscItems.Add("I"c, "first")   ' Adds a Char        ' ----- Now play them back in order.        displayText = miscItems("first") & " " & _           miscItems("second") & " " & miscItems("third")        MsgBox(displayText)     ' Displays "I am 25"     End Sub 

Version Differences

Visual Basic 2005 adds support for generics to several collection-style classes. Generics are discussed in Chapter 10.

See Also

Array Class, Hashtable Class, Queue Class, Stack Class




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net