Visual Basic - Objects - Collections
downx.gif (830 bytes)Propertiesdownx.gif (830 bytes)Methods

An alternative to Arrays
VB4: not Controls - >Control Array

Collections - Properties: Count

Count
The number of items (elements) in the collection.
Acts like the Ubound() function of an Array
> ListBox.ListCount
> Ex: Remove

Collections - Methods: Add, Remove, Item

Add
Put new member into a Collection object
Collection.Add Item, [Key], [Before], [After]
Item:= The new member
Key:= A text label used to identify the item
(the numeric index may also be used for identification)
Before:= Where to add the item - Index number (1..Count) or text label
After:= Where to add the item
  
Dim MyNames as New Collection
For i = 1 To 20
  MyNames.Add "Name" & i, "Key#" & i
Next
Print MyNames(3)
Print MyNames("Key#4")
 
Remove
Take member out of Collection
Collection.Remove Index | Key
Index:= A numeric expression
Key:= A string expression evaluating to a text label
  
Dim MyNames as New Collection
' Remove all elements (1)
For i = 1 To MyNames.Count
  MyNames.Remove 1 'the element index is adjusted after removal
Next
  
Dim MyNames as New Collection
' Remove all elements (2)
For Each x in MyNames
  MyNames.Remove 1
Next
 
Item
Returns a specific member of a collection, either by position or by key.
The default method for a Collection.
Collection.Item(index)
index:= A numeric (1..Count) or string (a Key label) expression
Dim Things As New Collection
---
Print Things.Item(3)
Print Things(4) '.Item is default method