Tuesday, 12 February 2013

Using ArrayList in VB.NET


Using ArrayList in  VB.NET

This example explains how to insert items into array list and fetch them based on type.

ArrayList is used for dynamic length 

Add namespace  System.Collections;

Create an Array List

Dim arrayList As ArrayList =  New ArrayList()

Add items to an array list


            arrayList.Add(1)
            arrayList.Add(2)
            arrayList.Add(3)
            arrayList.Add(1.1f)
            arrayList.Add(0.5f)
            arrayList.Add("Hello")
            arrayList.Add("World!")

It has integers and float values and string values also/

Get All values from Array List

            Console.WriteLine("Display All Values in Array List")
            Dim item As var
            For Each item In arrayList
                Console.Write(item+"\t")
            Next
Output: 1       2       3       1.1     0.5 Hello World!

Get Only Integers from Array List

            Console.WriteLine("Get Only Integers")
            Dim item As var
            For Each item In arrayList.OfType<int>()

                Console.Write(item+"\t")
            Next
Output:1       2       3
Get Only Float values from Array List

            Console.WriteLine("Get Only float")
            Dim item As var
            For Each item In arrayList.OfType<float>()

                Console.Write(item+"\t")
            Next
Output:1.1 0.5
Get Only String values from Array List

            Console.WriteLine("Get Only String Values")
            Dim item As var
            For Each item In arrayList.OfType<String>()

                Console.Write(item+"\t")
            Next
Output:Hello World!

Tags: using ArrayList in VB.NET,VB.NET ArrayList,ArrayList in VB.NET, ArrayList OfType method usage,Insert elements into Array List, Dynamic Array in VB.NET