VB.Net - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net Collections

VB.Net Collections

shape Description

Collections means a Group of Elements or Items which are stored in the memory. We can control the elements in the collections by using the index. The Collection is a type of class. All the types of collections are implemented from the namespace System.Collections.Following are the types of classes available in collections.
Classes Description
ArrayList Used to add and remove items from a list using an Index
SortedList Used as Key and Index to access the items in a list.
BitArray Used to represents an array of the Binary representation.
Queue Used to represent elements in a First In  First Out format.
Hashtable Used to access elements by using Key and Value.
Stack Used to represent elements in a Last In  First Out Format.

shape Conceptual figure

The following figure shows you the different types of collections.

ArrayList

shape Description

ArrayList is utilized  to add and remove items from a list using an Index.Following is an example to describe the ArrayList example. [vbnet]Module Module1 Sub Main() Dim al As ArrayList = New ArrayList() Dim i As Integer Console.WriteLine("Inserting some numbers:") al.Add(45) al.Add(78) al.Add(33) al.Add(56) al.Add(12) al.Add(23) al.Add(9) Console.WriteLine("Capacity: {0} ", al.Capacity) Console.WriteLine("Count: {0}", al.Count) Console.Write("Content: ") For Each i In al Console.Write("{0} ", i) Next i Console.WriteLine() Console.Write("Sorted Content: ") al.Sort() For Each i In al Console.Write("{0} ", i) Next i Console.WriteLine() Console.ReadKey() End Sub End Module [/vbnet] Output:

SortedList

shape Description

SortedList is utilized as Key and Index to access the items in a list.Following is an example which describes more concept regarding SortedList. [vbnet]Module Module1 Sub Main() Dim sl As SortedList = New SortedList() sl.Add("001", "Zara Ali") sl.Add("002", "Abida Rehman") sl.Add("003", "Joe Holzner") sl.Add("004", "Mausam Benazir Nur") sl.Add("005", "M. Amlan") sl.Add("006", "M. Arif") sl.Add("007", "Ritesh Saikia") If (sl.ContainsValue("Nuha Ali")) Then Console.WriteLine("This student name is already in the list") Else sl.Add("008", "Nuha Ali") End If ' Get a collection of the keys. Dim key As ICollection = sl.Keys Dim k As String For Each k In key Console.WriteLine(" {0} : {1}", k, sl(k)) Next k Console.ReadKey() End Sub End Module[/vbnet] Output:

BitArray

shape Description

BitArray is utilized to represents an array of the Binary representation.Following is an example which describes more about BitArray. [vbnet]Module Module1 Sub Main() 'creating two bit arrays of size 8 Dim ba1 As BitArray = New BitArray(8) Dim ba2 As BitArray = New BitArray(8) Dim a() As Byte = {60} Dim b() As Byte = {13} 'storing the values 60, and 13 into the bit arrays ba1 = New BitArray(a) ba2 = New BitArray(b) 'content of ba1 Console.WriteLine("Bit array ba1: 60") Dim i As Integer For i = 0 To ba1.Count Console.Write("{0 } ", ba1(i)) Next i Console.WriteLine() 'content of ba2 Console.WriteLine("Bit array ba2: 13") For i = 0 To ba2.Count Console.Write("{0 } ", ba2(i)) Next i Console.WriteLine() Dim ba3 As BitArray = New BitArray(8) ba3 = ba1.And(ba2) 'content of ba3 Console.WriteLine("Bit array ba3 after AND operation: 12") For i = 0 To ba3.Count Console.Write("{0 } ", ba3(i)) Next i Console.WriteLine() ba3 = ba1.Or(ba2) 'content of ba3 Console.WriteLine("Bit array ba3 after OR operation: 61") For i = 0 To ba3.Count Console.Write("{0 } ", ba3(i)) Next i Console.WriteLine() Console.ReadKey() End Sub End Module[/vbnet] Output:

Queue

shape Description

Queue is utilized to represent elements in a First In  First Out format.Following is example which describes more. [vbnet]Module Module1 Sub Main() Dim q As Queue = New Queue() q.Enqueue("A") q.Enqueue("M") q.Enqueue("G") q.Enqueue("W") Console.WriteLine("Current queue: ") Dim c As Char For Each c In q Console.Write(c + " ") Next c Console.WriteLine() q.Enqueue("V") q.Enqueue("H") Console.WriteLine("Current queue: ") For Each c In q Console.Write(c + " ") Next c Console.WriteLine() Console.WriteLine("Removing some values ") Dim ch As Char ch = q.Dequeue() Console.WriteLine("The removed value: {0}", ch) ch = q.Dequeue() Console.WriteLine("The removed value: {0}", ch) Console.ReadKey() End Sub End Module[/vbnet] Output:

Hashtable

shape Description

Hashtable is utilized  to access elements by using Key and Value.Following is an example to describe more. [vbnet]Module Module1 Sub Main() Dim ht As Hashtable = New Hashtable() Dim k As String ht.Add("001", "Zara Ali") ht.Add("002", "Abida Rehman") ht.Add("003", "Joe Holzner") ht.Add("004", "Mausam Benazir Nur") ht.Add("005", "M. Amlan") ht.Add("006", "M. Arif") ht.Add("007", "Ritesh Saikia") If (ht.ContainsValue("Nuha Ali")) Then Console.WriteLine("This student name is already in the list") Else ht.Add("008", "Nuha Ali") End If ' Get a collection of the keys. Dim key As ICollection = ht.Keys For Each k In key Console.WriteLine(" {0} : {1}", k, ht(k)) Next k Console.ReadKey() End Sub End Module [/vbnet] Output:

Stack

shape Description

Stack is utilized  to represent elements in a Last In  First Out Format.Following is an example which describes more. [vbnet]Module Module1 Sub Main() Dim st As Stack = New Stack() st.Push("A") st.Push("M") st.Push("G") st.Push("W") Console.WriteLine("Current stack: ") Dim c As Char For Each c In st Console.Write(c + " ") Next c Console.WriteLine() st.Push("V") st.Push("H") Console.WriteLine("The next poppable value in stack: {0}", st.Peek()) Console.WriteLine("Current stack: ") For Each c In st Console.Write(c + " ") Next c Console.WriteLine() Console.WriteLine("Removing values ") st.Pop() st.Pop() st.Pop() Console.WriteLine("Current stack: ") For Each c In st Console.Write(c + " ") Next c Console.ReadKey() End Sub End Module[/vbnet] Output:

Summary

shape Points

  • Count is the property of Stack that utilized to track number of elements from the stack.
  • Stack represents last in first out mechanism.
  • Queue represents first in first out mechanism.
  • To implement an associative array hash table will be utilized.