Export (0) Print
Expand All

Collections as an Alternative to Arrays

Although collections are most often used for working with objects, you can use a collection to work with any data type. In some circumstances, it can be more efficient to store items in a collection than in an array.

You might want to use a collection if you are working with a small, dynamic set of items. The following code fragment shows how you might use a collection to save and display a list of URL addresses.

' Module-level collection declaration. 
Public URLHistory As New Microsoft.VisualBasic.Collection 
' ... 
' Code for adding a specified URL address to the collection. 
Private Sub SaveURLHistory(ByVal URLAddress As String) 
End Sub 
' ... 
' Code for displaying the list of URL addresses in the Immediate window. 
Private Sub PrintURLHistory() 
   Dim URLAddress As String 
   For Each URLAddress In URLHistory 
   Next URLAddress 
End Sub 

See Also

Arrays Overview | Array Usage | Declaring Array Variables | Advanced Features of Arrays | Arrays of Arrays

© 2015 Microsoft