VB 6 Collections

In the previous article, you learned about different types of loops and know about arrays. In VB 6, there is also a container called Collection.

Advertisements

The collections are interesting because they have following properties:

  • Can contain different data types
  • Have their own index
  • Have their own methods

The collection have different types of methods to manage the collection and its items. They are listed below.

Add – to add items to collection

Remove – to remove an item from collection

Count – to count number of items in the collection.

Advertisements

Item – access any item in the collection using its index.

Example Program: Collection

In this program we will accept the items through a textbox and use Add method of collection. We can display and show the collection items respectively.

Dim myCollection As New Collection
Dim studentName As String, i As Integer

Private Sub Command1_Click()

myCollection.Add (Text1.Text)
Text1.Text = ""

End Sub

Private Sub Command2_Click()

For i = 1 To myCollection.Count

studentName = myCollection.Item(i)
MsgBox (studentName)

Next i

End Sub

Private Sub Command3_Click()

MsgBox (myCollection.Count)

End Sub

Output – Collection

Add item to the collection from the text box.

Figure 1 - Add Item to the Collection
Figure 1 – Add Item to the Collection

Show the item from the collection by clicking on the Show button. All the items will be displayed one at a time.

Figure 2 - Show items from the collection one at a time.
Figure 2 – Show items from the collection one at a time.

Display the number of items in the collection.

Figure 3 - Number of items in the collection
Figure 3 – Number of items in the collection

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.