Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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) 
   URLHistory.Add(URLAddress) 
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 
      Debug.WriteLine(URLAddress) 
   Next URLAddress 
End Sub 

See Also

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.