Collections
Collections
Visual Basic
Collections
Presented By:
Ms Anita Saini
AP, BCA Department
Content to be cover
Introduction of Collections
Declaration of Collections
Collection Characteristics
Adding Items in a Collection
Removing Items in a Collection
Returning Items in a Collection
Counting Items in a Collection
Introduction of Collections
A group of logically related items
To easily 'get at' each item
Collection class provides an array-like container
more flexible than an array
It is Object Oriented Nature
Collections contain items: values or objects
Objects that have Properties and Methods
Collection access its items via a key.
Declaration
Declare a collection variable:
Dim Temperatures As New Collection
Declaration of Arrays:
Dim Salary(15) As Integer
Salary(0) = 30000
Salary(1) = 40000
Salary(2) = 50000
Collection Characteristics
It is considered an Object
It to possess Properties and Methods
Collection object : Three Methods and One
Property
- Add Method : Adds Items
- Remove Method : Deletes an items by index
or key
- Item Method : Returns an item by index or key
- Count Property : Returns the number of Items
Collection Characteristics
Visual Basic Collections Types:
- Systems Collections
- Programmer defined Collections.
In Systems Collections :
- Forms Collection and Controls Collection.
Visual Basic automatically 'adding' newly
loaded Forms to the Forms Collection
Controls you place on the form - automatically
'added' to the Controls Collection.
Adding Items in a Collection
Add Method : adds new items to the collection.
Syntax :
Collection.Add value,[key],[before],[after]
e.g: Temperatures.Add 78, “KUK”
Temperatures.Add 79, “YNR”, , “KUK”
Temperatures.Add 79, “YNR”, , after : = “KUK”
Collections aren’t sorted
Removing Items in a Collection
Remove Method : remove an item
Object.Remove key
Object.Remove index
e.g : Temperatures.Remove “KUK”
Temperatures.Remove 6
Returning Items in a Collection
Item Method :
Object.Item key
Object.Item index
e.g : Temperatures.Item(“YNR”)
It is the default method
So, omit it when access an item in a collection
like, Temperatures(“YNR”)
Counting Items in a Collection
Conut Property : returns the number of items
Object.Count
Count use to scan all the elements of the
collection with a For ......Next Loop
like,
To store the city names and temperatures using
collections