VB6 Tutorial 54: Collections
No attachments for this post
Collections are objects in Visual Basic that are used to store a group of data values. Collections are similar to array. But there are some special features that differentiate the collections from arrays.
We have seen that array elements must be of the same data type. But the Collection members can be of any data type and you don't need to set the size of the Collection object. You can easily add items to the collection and it will grow accordingly.
Creating a Collection
To use a Collection in your code, you first need to declare and create it.
Dim names As Collection 'Declaring the Collection Set names = New Collection 'Creating the Collection
Or, replace the above code with this one line code
Dim names As New Collection 'Declaration and creation
The Add method
You can add one item at a time using the Add method.
Dim names As New Collection names.Add "john" names.Add "david"
You may use a string key associated with the item. The string key is used to refer to a Collection item.
Dim names As New Collection names.Add "John", "one" ' "one" is the string key, used to refer to the item
The Item method
You can refer to a particular item of the collection using the Item method. Here you may use either the index or key value. The index value starts from 1.
Retrieving a particular item using the index of the item
Private Sub cmdShow_Click() Dim names As New Collection names.Add "John", "one" names.Add "David" Print names.Item(2) '2 is the index of the item End Sub
Retrieving a particular item using the string key of the item
Private Sub cmdShow_Click() Dim names As New Collection names.Add "John", "one" names.Add "David" Print names.Item("one") End Sub
The Item method is the default member of the Collection class, so you may omit it in your code.
Private Sub cmdShow_Click() Dim names As New Collection names.Add "John", "one" names.Add "David" Print names("one") Print names(2) End Sub
Before and After
You can choose to store the item values exactly where you want using the Before and After argument of the Add method.
Private Sub cmdShow_Click() Dim items As New Collection items.Add "one" items.Add "two" items.Add "three", , 1 For i = 1 To 3 Print items.Item(i) Next i End Sub
three one two
You can remove a particular item from the Collection using the Remove method.
Private Sub Command1_Click() Dim country As New Collection country.Add "USA" country.Add "UK" country.Add "Japan", "j" country.Remove (1) country.Remove ("j") End Sub
Private Sub cmdCount_Click() Dim country As New Collection country.Add "USA" country.Add "India" country.Add "Japan", "j" Dim n As Integer n = country.Count Print n End Sub
Retrieving the last item
Private Sub Command1_Click() Dim country As New Collection country.Add "Germany" country.Add "India" country.Add "China", "j" Print country.Item(country.Count) End Sub
Delete all items
Use a Do While loop to delete all item from the Collection object.
Private Sub cmdDeleteAll_Click() Dim country As New Collection country.Add "Bangladesh" country.Add "Australia" country.Add "Russia", "j" Do While (country.Count > 0) country.Remove 1 Loop Print country.Count End Sub
Another way to delete all items is to destroy the Collection object. The following code destroys the Collection object and thus deletes all the items.
Set items = Nothing
Set items = New Collection
Comments on this post
You must be logged in to make a comment.
AnonymousCoward posted this comment on 2020-09-28:
I never see a tutorial as clear as this on this subject... but, still u does not gives more details about the arguments feed at the ADD method... I did not get the function of some of it... in the case of the "names" collection... what is the porpouse of the "one" word? "david","one" ?
what is it?