Download! Download Point responsive WP Theme for FREE!

Collection Class in Vb.net

ArrayList Class

Collection Class in VB.NET includes Arraylist Class, Hashtable, Stack, Queue, SortedList Class, Bit Array

Following are the examples of above Collection Class in Vb.net

EXAMPLE 1

Module Module1
    Sub Main()
	' Create an ArrayList and add two elements.
	Dim list1 As New ArrayList
	list1.Add(5)
	list1.Add(7)
	' Create a separate ArrayList.
	Dim list2 As New ArrayList
	list2.Add(10)
	list2.Add(13)
	' Add this ArrayList to the other one.
	list1.AddRange(list2)
	' Loop over the elements.
	Dim num As Integer
	For Each num In list1
	    Console.WriteLine(num)
	Next
    End Sub
End Module

EXAMPLE 2

Module Module1
    Sub Main()
	' Add two elements to the ArrayList.
	Dim list As New ArrayList
	list.Add(9)
	list.Add(10)
	' Write the Count.
	Console.WriteLine(list.Count)
	' Clear the ArrayList.
	list.Clear()
	' Write the Count again.
	Console.WriteLine(list.Count)
    End Sub
End Module

EXAMPLE 3

Module Module1
    Sub Main()
	' Create an ArrayList and add three strings to it.
	Dim list As New ArrayList
	list.Add("aits")
	list.Add("clg")
	list.Add("rajkot")
	' Remove a string.
	list.RemoveAt(1)
	' Insert a string.
	list.Insert(0, "hello")
	' Remove a range.
	list.RemoveRange(0, 2)
	Dim str As String
	For Each str In list
	    Console.WriteLine(str)
	Next
    End Sub

EXAMPLE 4

Module Module1
    Sub Main()
	' Create new ArrayList.
	Dim list1 As New ArrayList
	list1.Add("abc")
	list1.Add("def")
	list1.Add("pqr")
	list1.Add("xyz")
	' Create a new ArrayList and fill it with the range from the first one.
	Dim list2 As New ArrayList
	list2 = list1.GetRange(2, 2)
	' Loop over the elements.
	Dim str As String
	For Each str In list2
	    Console.WriteLine(str)
	Next
    End Sub
End Module

 HASH TABLE

EXAMPLE 1

Module Module1
    Sub Main()
	' Create an example Hashtable instance.
	Dim table As Hashtable = New Hashtable
	table(1) = "One"
	table(2) = "Two"
	table(15) = "Fifteen"

	' Use For Each loop over the Hashtable.
	For Each element As Object In table
	    Console.WriteLine(element.Key)
	    Console.WriteLine(element.Value)
	Next
    End Sub
End Module

EXAMPLE 2

Module Module1
    Sub Main()
	Dim table As Hashtable = New Hashtable
	table.Add(300, "Clg")
	table.Add("Area", 1000)

	' Get string and integer values.
	Dim value1 As String = table(300)
	Dim value2 As Integer = table("Area")

	' Display values.
	Console.WriteLine(value1)
	Console.WriteLine(value2)
    End Sub		
End Module
***********************************************
Module Module1
    Sub Main()
	Dim table As Hashtable = New Hashtable
	table.Add(100, "Hello")

	' Get string and integer values.
	Dim value1 = table(100)

	' Use TypeOf operator to test type of hashtable value.
	If (TypeOf value1 Is String) Then
	    ' Cast.
	    Dim value As String = value1
	    Console.WriteLine(value)
	End If
    End Sub
End Module

EXAMPLE 3

Module Module1
    Sub Main()
	Dim table As Hashtable = New Hashtable
	table.Add(100, "hello")
	table.Add(200, "aits")
	table.Add(300, "clg")

	' Current count.
	Console.WriteLine(table.Count)
	' Clear.
	table.Clear()
	' Current count is now zero.
	Console.WriteLine(table.Count)
    End Sub
End Module

EXAMPLE 4

Module Module1
    Function GetHashtable() As Hashtable
	Dim table As Hashtable = New Hashtable
	table.Add("Area", 1000)
	table.Add("Perimeter", 55)
	table.Add("Mortgage", 540)
	Return table
    End Function

    Sub Main()
	' Create an example Hashtable instance.
	Dim table As Hashtable = GetHashtable()

	' Test Contains* functions.
	Console.WriteLine(table.ContainsKey("Perimeter"))
	Console.WriteLine(table.Contains("Area"))
	Console.WriteLine(table.ContainsValue(55))

	' Look up area.
	Dim area As Integer = table("Area")
	Console.WriteLine(area)
    End Sub
End Module

Stack

EXAMPLE 1

Module Module1
    Sub Main()
	Dim stack As Stack(Of Integer) = New Stack(Of Integer)
	stack.Push(10)
	stack.Push(100)
	stack.Push(1000)

	' Display stack elements.
	For Each value As Integer In stack
	    Console.WriteLine(value)
	Next
    End Sub
End Module

EXAMPLE 2

Module Module1
    Sub Main()
	Dim stack As Stack(Of Integer) = New Stack(Of Integer)
	stack.Push(10)
	stack.Push(100)
	stack.Push(1000)

	' Test Pop.
	Dim value As Integer = stack.Pop
	Console.WriteLine("Element popped: {0}", value)

	' Test Peek.
	value = stack.Peek
	Console.WriteLine("Element peeked: {0}", value)
    End Sub
End Module

Queue

EXAMPLE 1

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

 SortedList Class

EXAMPLE 1

Module Module1

    Sub Main()

        Dim mySL As New SortedList()
        mySL.Add("2", "!")
        mySL.Add("b", "qqq")
        mySL.Add("04", "World")
        mySL.Add("a", "Hello")

        ' Displays the properties and values of the SortedList.
        Console.WriteLine("mySL")
        Console.WriteLine("  Count:    {0}", mySL.Count)

        Console.WriteLine("  Keys and Values:")

        PrintKeysAndValues(mySL)

    End Sub


    Public Sub PrintKeysAndValues(ByVal myList As SortedList)

        Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & "-VALUE-")

        Dim i As Integer
        For i = 0 To myList.Count - 1
            Console.WriteLine(ControlChars.Tab & "{0}:" & _
 ControlChars.Tab & "{1}", myList.GetKey(i), myList.GetByIndex(i))
        Next i

        Console.WriteLine()
   End Sub 
End Module

 BitArray

EXAMPLE 1

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 = {3}
        Dim b() As Byte = {1}

        ba1 = New BitArray(a)
        ba2 = New BitArray(b)
        'content of ba1
        Console.WriteLine("Bit array ba1: 3")
        Dim j As Integer
        For j = 0 To ba1.Count - 1
            Console.Write("{0} ", ba1(j))
        Next j


        Console.WriteLine()
        'content of ba2
        Console.WriteLine("Bit array ba2: 1")
        For j = 0 To ba2.Count - 1
            Console.Write("{0} ", ba2(j))
        Next j

        Console.WriteLine()
        Dim ba3 As BitArray = New BitArray(8)
        ba3 = ba1.And(ba2)
        'content of ba3
        Console.WriteLine("Bit array ba3 after AND operation: ")
        For j = 0 To ba3.Count - 1
            Console.Write("{0}", ba3(j))
        Next j
        Console.WriteLine()
        Dim ba4 As BitArray = New BitArray(16)
        ba4 = ba1.Or(ba2)
        'content of ba3
        Console.WriteLine("Bit array ba4 after OR operation: ")
        For j = 0 To ba4.Count - 1
            Console.Write("{0} ", ba4(j))
        Next j
        Console.WriteLine()
    End Sub
End Module

Download

Collection Class Word Document