Collection in Visual Basic
Collection:
Visual Basic provides an alternative of an array in the form of collection. In case of array elements are accessed by its index number but elements of a collection are accessed by using key that is unique for each element and also by index number. A collection is declared as
Dim CityName As New Collection
The word New tells Visual Basic to create new collection with CityName. The object provides four methods.
Add:
Add method is used to add new item in a collection syntax of Add Method is as under
Collection.Add Value, Key, Beforekey, Afterkey
The argument Value represents actual item to be added in a collection and key argument represents the key for storing item. Beforekey and Afterkey arguments represents the key before or after which new item is added, but one of them can be used at a time e.g.
CityName.Add "D.I.Khan","key2","key1"
The item 'D.I.khan' is added in the CityName collection with key "key2" and before the element of key "key1"
Remove:
Remove method is used to delete an item from collection by using an index number or key value. e.g.
CityName.Remove (2) 'used of index number
CityNameRemove ("key2") 'used of key
Item:
Using index number or a key uses, Item method to return an item of the collection e.g.
CityName.item(1) 'refers to first item
CityName.item("key2") 'refers to item with key "key2"
Count:
Count method is used to return the total items of a collection. e.g.
CityName.Count 'refers to total elements of CityName collection
Examples:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and set the properties of controls according to the following table.
Control Name | Property | Value |
Form | WindowState | 2-Maximized |
Command1 | Caption | Enter New CityName in Collection |
Command2 | Caption | Count total element in Collection |
Command3 | Caption | Remove an item From collection |
Command4 Related Articles | Caption | View an item of Collection |
Command5 | Caption | View all elements of collection |
Command6 | Caption | View elements of Collection by using key |
Create six buttons with the upper Names.
Write the following coding in Click events of each CommanButton control and in the Load event of the Form control and in General Section.
‘In General Section Dim CityName As New Collection Dim c As Integer |
Private Sub Command1_Click () Dim cname As String Cname = InputBox (“Enter New CityName”) CityName.Add cname, “key” + CStir(c) C = c + 1 End Sub |
Private Sub Command2_Click () Print “Total Element in CityName = “CityName.Count End Sub |
Private Sub Command3_Click () On error Resum Next Dim index As String Index = InputBox (“Enter index for delete the element” If index < 1 0r index > CityName.Count Then MsgBox (“Invalid index number”) Else CityName.Remove (Val(index)) End If End Sub |
Private Sub Command4_Click () On error Resum Next Dim index As String Index = InputBox (“Enter index for print the element” If index = “” then Exit Sub End If If Index < 1 or index > CityName.Count Then MsgBox (“Invalid index number”) Else Print CityName.Remove (Val(index)) End If End Sub |
Private Sub Command5_Click () For Each cname in CityName Print cname Next End Sub |
Private Sub Command6_Click () On error GoTo lab1 Dim key As String key = InputBox (“Enter index for printed the element” If key = “” then Exit Sub End If If CityName.Count > 1 Then Print CityName.item (Key) Else Print “No Element Exist” End If Exit Sub Lab1: MsgBox (“Key Not Valid”) End Sub |
Press F5 to run this project and press each CommandButton to check its output.