NamespaceMicrosoft.VisualBasic CreatableYes SyntaxDim result As [New] Collection DescriptionA 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.
Usage at a Glance
ExampleThis 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 DifferencesVisual Basic 2005 adds support for generics to several collection-style classes. Generics are discussed in Chapter 10. See AlsoArray Class, Hashtable Class, Queue Class, Stack Class |