VB.net Collection – Various Collection Classes in VB.net

What is Collection in VB.net?

The VB.net Collection is helpful for managing a group of objects in a flexible way so that we may dynamically conduct different actions like insert, update, delete, get, etc. on object pieces based on our requirements.

Typically, while developing visual basic apps, we will be asked to create or manage a collection of connected objects.

In such a situation, using arrays and collections are the two methods we have to build group objects in Visual Basic.

The collection is a class in Visual Basic, so before we can add to, delete from, or otherwise modify the defined collection, we must first declare an instance of the class.

Additionally, because the collections are implemented using the IEnumerable interface, we can access collection items using a foreach loop.

Collection classes in VB.net are focused classes for storing and retrieving data.

Support for stacks, queues, lists, and hash tables is provided by these classes. The same interfaces are implemented by most collection classes.

VB.net Collection classes have a variety of uses, including dynamic memory allocation for elements and index-based access to lists of things.

These classes build collections of objects belonging to the Object class, the VB.Net data types’ base class.

Various Collection Classes in VB.net and Their Usage

The System.Collection namespace’s various frequently used classes are listed below.

1. ArrayList

It represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array.

However, unlike an array, you can add and remove items from a list at a specified position using an index and the array resizes itself automatically.

It also allows dynamic memory allocation, adding, searching, and sorting items in the list.

Example Program of ArrayList in VB.net:

The following example demonstrates the concept.

Sub Main()
      Dim al As ArrayList = New ArrayList()
      Dim i As Integer
      Console.WriteLine("Adding 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

When the above code is compiled and executed, it produces the following result:

Adding some numbers:
Capacity: 8
Count: 7
Content: 45 78 33 56 12 23 9
Content: 9 12 23 33 45 56 78

2. Hashtable

It uses a key to access the elements in the collection. A hash table is used when you need to access elements by using key, and you can identify a useful key value.

Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

Example Program of Hashtable in VB.net:

The following example demonstrates the concept.

Module collections
   Sub Main()
      Dim ht As Hashtable = New Hashtable()
      Dim k As String
      ht.Add("001", "Paul")
      ht.Add("002", "Ted Ted")
      ht.Add("003", "Jaymar")
      ht.Add("004", "Glenn")
      ht.Add("005", "Prince")
      ht.Add("006", "Adones")
      ht.Add("007", "Jude")
      
      If (ht.ContainsValue("Caren")) Then
         Console.WriteLine("This student name is already in the list")
      Else
          ht.Add("008", "Caren")
      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

When the above code is compiled and executed, it produces the following result:

006: Adones
007: Jude
008: Caren
003: Jaymar
002: Ted Ted
004: Glenn
001: Paul
005: Prince

3. SortedList

It uses a key as well as an index to access the items in a list. A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index.

If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.

Example Program of SortedList in VB.net:

The following example demonstrates the concept.

Module collections
   Sub Main()
      Dim sl As SortedList = New SortedList()
      sl.Add("001", "Paul")
      sl.Add("002", "Ted Ted")
      sl.Add("003", "Jaymar")
      sl.Add("004", "Glenn")
      sl.Add("005", "Prince")
      sl.Add("006", "Adones")
      sl.Add("007", "Jude")
      
      If (sl.ContainsValue("Caren")) Then
         Console.WriteLine("This student name is already in the list")
      Else
         sl.Add("008", "Caren")
      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

When the above code is compiled and executed, it produces the following result:

001: Paul
002: Ted Ted
003: Jaymar
004: Glenn
005: Prince
006: Adones
007: Jude
008: Caren

4. Stack

It represents a last-in, first-out collection of objects. It is used when you need a last-in, first-out access to items.

When you add an item to the list, it is called pushing the item, and when you remove it, it is called popping the item.

Example Program of Stack in VB.net:

The following example demonstrates the use of Stack.

Module collections
   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

When the above code is compiled and executed, it produces the following result:

Current stack:
W G M A
The next poppable value in stack: H
Current stack:
H V W G M A
Removing values
Current stack:
G M A

5. Queue

It represents a first-in, first-out collection of objects. It is used when you need a first-in, first-out access of items.

When you add an item to the list, it is called enqueue, and when you remove an item, it is called deque.

Example Program of Queue in VB.net:

The following example demonstrates the use of Queue.

Module collections
   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

When the above code is compiled and executed, it produces the following result:

Current queue:
A M G W
Current queue:
A M G W V H
Removing some values
The removed value: A
The removed value: M

6. BitArray

It represents an array of the binary representation using the values 1 and 0. It is used when you need to store the bits but do not know the number of bits in advance.

You can access items from the BitArray collection by using an integer index, which starts from zero.

Example Program of BitArray in VB.net:

The following example demonstrates the use of the BitArray class.

Module collections
   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

When the above code is compiled and executed, it produces the following result:

Bit array ba1: 60
False False True True True True False False
Bit array ba2: 13
True False True True False False False False
Bit array ba3 after AND operation: 12
False False True True False False False False
Bit array ba3 after OR operation: 61
True False True True False False False False

Summary

In this tutorial, we’ve discussed the vb.net Collections and their function, Collections in vb.net have different various that are given in this tutorial with the provided Example Program.


Leave a Comment