Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

29 Collections


'Collections are objects which can contain many other objects, not necessarily
'of the same type.  Limitation - collections cannot contain UDTs.

Dim MyCollection As New Collection

'Collections have these properties/methods:
..Add   'add an item to the collection
.Count   'number of objects in the collection
.Item   'reference to the object in the collection
.Remove   'removes the item from the collection

'members of a collection have an index and a key
Key   'unique string value
Index  'Long integer between 1 and number of items in the collection

'refer to an item in a Collection in these ways:
MyCollection(Key)
MyCollection(Index)
'or
MyCollection.Item(Key)
MyCollection.Item(Index)

'iterate over a collection in two ways:
For i = 1 To MyCollection.Count
  'do something with MyCollection(i)
Next i

Dim X As Object
For Each X In MyCollection
 ... Do something With X
Next

'set a reference to an object in a collection this way
Set MyVar = MyCollection.Item(Index)

'to remove an item from a collection
MyCollection.Remove "Name"
MyCollection.Remove 2

'to clear a collection
Set MyCollection = Nothing