Visual Basic .NET Collections are the data structures which hold the data in different ways for the operations. The important data structures in the collections are
- ArrayList
- HashTable
- Stack
- Queue
- Arrays
- Dynamic Arrays
ArrayList
It is the most flexible data structure in the VB.NET collections. It stores data in an array that can be dynamically resized.
ArrayList Functions
- Add()
- Insert()
- Remove()
- RemoveAt()
- Sort()
Add
The Add method appends the value in the end of the ArrayList Data structure. It is not needed to check for the room before adding the element.
Syntax
ArrayList.add(Item)
ArrayList.insert(index,item)
ArrayList.Remove(item)
ArrayList.RemoveAt(index)
Example
'Declare the IList ArrayList
Dim IList As New ArrayList
Dim i As Integer
'Add an Item in the ArrayList
IList.Add("MyItem1")
IList.Add("MyItem2")
IList.Add("MyItem3")
IList.Add("MyItem4")
IList.Add("MyItem5")
'Loop will print the items.
'IList.Count will return the total number of items in the IList
For i = 0 To IList.Count - 1
MsgBox(IList.Item(i))
Next
'insert an item
IList.Insert(2, "Item6")
'sort itemms in an arraylist
IList.Sort()
'remove an item
IList.Remove("MyItem1")
'remove item from a specified index
IList.RemoveAt(3)
MsgBox("After Insert,Sort and Remove")
For i = 0 To IList.Count - 1
MsgBox(IList.Item(i))
Next
The above example stores five items in the Array List then displays all the items using the Message box. After, it inserts one more item at the third position of the Array List then removes the “MyItem1” item from the list using the Remove method. Again it removes the item at the third position using RemoveAt function.
HashTable
Hash Table stores the data as key-value pairs. We can retrieve items from the hash table with the help of the keys.
HashTable Functions
- Add()
- ContainsKey()
- ContainsValue()
- Remove()
Syntax
HashTable.Add(Key,Value)
HashTable.ContainsKey(key)
HashTable.ContainsValue(Value)
HashTable.Remove(Key)
Example
Dim MyWeeks As New Hashtable
Dim day As DictionaryEntry
MyWeeks.Add("1", "Sunday")
MyWeeks.Add("2", "Monday")
MyWeeks.Add("3", "Tuesday")
MyWeeks.Add("4", "Wednesday")
MyWeeks.Add("5", "Thursday")
MyWeeks.Add("6", "Friday")
MyWeeks.Add("7", "Saturday")
'Display a single Item
MsgBox(MyWeeks.Item("2"))
'Search for an Item
If MyWeeks.ContainsValue("Tuesday") Then
MsgBox("Found")
Else
MsgBox("Not Found")
End If
'Remove an Item
MyWeeks.Remove("2")
'Display all key value pairs
For Each day In MyWeeks
MsgBox(day.Key & " -- " & day.Value)
Next
In the above example we added weekdays to a Hash Table and displayed the item with the key 2. Then we checked out whether an Item with a value “Tuesday” exists or not. Next we removed the item with the key 2 from the Hash Table. Finally I displayed all the items in the Hash Table.
Stack
A Stack is another easy to use collection in VB.NET. Stack follows the “Last In First Out” (LIFO) System. i.e. stack returns the last item first. It provides Push and Pop methods to store and retrieve data from the stack.
Stack Functions
- Push() - adds an item to the top position of the stack
- Pop() - removes the item at the top of the stack
- Contains() – looks into the stack for an item and returns true if the item exists otherwise returns false
Syntax
Stack.Push(Object)
Stack.Pop()
Stack.Contains(Object)
Example
Dim STbl As New Stack
STbl.Push("One")
STbl.Push("Two")
STbl.Push("Three")
STbl.Push("Four")
STbl.Push("Five")
STbl.Push("Six")
STbl.Push("Seven")
If STbl.Contains("Four") Then
MsgBox(STbl.Count)
STbl.Pop()
MsgBox(STbl.Count)
STbl.Push("Eight")
MsgBox(STbl.Count)
Else
MsgBox("Not Exist")
End If
In the above example we have created a stack STbl with seven items. We have looked for an item with the value “four”. If exists then displayed the count of the items in the stack. The value displayed is 7. Next using pop() removed the last element from the list and displayed the number of items in the stack. The value displayed now is 6. And then using the Push() method we have added another element “Eight” at the top. Again we have displayed the number of items in the stack. Now it shows 7.
Queue
Queue works in the “First In First Out” (FIFO) method. The Item that first enters the queue is the first to leave the queue. Below are the methods to manipulate the items in a queue object.
Queue Functions
Enqueue - Adds an item to the Queue.
Syntax
Queue.Enqueue(Object)
Dequeue - Removes an item from the Queue.
Syntax
Queue.Dequeue()
Peek - Get the reference of the oldest item
Syntax
Queue.Peek()
Example
Dim MyQueue As New Queue
MyQueue.Enqueue("January")
MyQueue.Enqueue("February")
MyQueue.Enqueue("March")
MyQueue.Enqueue("April")
MyQueue.Enqueue("May")
MyQueue.Enqueue("June")
MyQueue.Enqueue("July")
If MyQueue.Contains("April") Then
MsgBox("Contains April ")
MyQueue.Dequeue()
MsgBox(MyQueue.Contains("January"))
MsgBox(MyQueue.Peek())
Else
MsgBox("Not Contains April ")
End If
When you execute the above code, it will add seven items to the Queue. Then it checks out if an item “April” exists in the Queue or not. If it exists then it will show the message “Contains April”. It then removes the oldest item from the queue using the Dequeue() function. Here the oldest item is “January”. Now it looks into the queue for the item “January” using the Contains (). Since it is already removed from the queue the Contains () function returns false to indicate that the item does not exist in the queue. Then using Peek() method we display the oldest item. Now our oldest item in the queue is “February”
Arrays
Arrays are used to store a group or set of similar data type elements as a single unit.
Syntax
Dim myArray(5) as string
Here the array “myArray” is declared as a string type and it has the capacity of storing six items in it.
Example
Dim i As Integer
Dim num(6) As String
num(0) = "One"
num(1) = "Two"
num(2) = "Three"
num(3) = "Four"
num(4) = "Five"
num(5) = "Six"
num(6) = "Seven"
For i = 0 To num.Length - 1
MsgBox(num(i))
Next
When you execute the above program you will get the Numbers from “One” to “Seven”
Dynamic Arrays
Dynamic Arrays are similar to Arrays but we can resize the dynamic arrays at runtime. Dynamic arrays are very useful in certain scenarios when we cannot predict the size of the array in advance or during the design.
Array Declaration
Dim MyArray() as string
Resizing
Redim MyArray(5)
Now we can store six items in the array “MyArray”
Redim Preserve MyArray(5)
It will keep the existing items in the array “MyArray”, and we can insert another six items in the array.
Example
Dim i As Integer
Dim MyArray() As Integer
ReDim MyArray(1)
MyArray(0) = 10
MyArray(1) = 20
For i = 0 To MyArray.Length - 1
MsgBox(MyArray(i))
Next
ReDim Preserve MyArray(2)
MyArray(2) = 40
For i = 0 To MyArray.Length - 1
MsgBox(MyArray(i))
Next
When we execute the above code, the display first shows two values stored in an array. The second loop will display three values.