Tuesday, March 20, 2012

Converting Arraylist to Array using VB.NET

Author : Prakash Pradeep Gopu


Array List :An ArrayList class represents a dynamically sized, index-based collection of objects. In the cases where you don't know the number of objects that you would store, an ArrayList class is a good option to utilize.

Declaring Array list:
SynTax :
Dim <Arraylistname> As ArrayList = New ArrayList()
Example :
Dim Name As ArrayList = New ArrayList()

Add an Item to array : 
 For adding an Item in to arraylist use Add(). For removing an Item in arraylist use Remove().
Synatax: 
<arrayname>.Add(<ItemVale>)
<arrayname>. Remove (<ItemVale>)

Example :
Name.Add("Prakash")
Name.Remove("Prakash")
The following program will converting array list values to array for this ToArray() method.
Module Module1

    Sub Main()
        'Declaring an Array list
        Dim Name As ArrayList = New ArrayList()
        'Adding items to arraylist
        Name.Add("Prakash")
        Name.Add("Pradeep")
        Name.Add("Hutch")
        Name.Add("Sandya")
        'remove existing arraylist item
        Name.Remove("Prakash")
        'converting arraylist to array
        Dim array() As Object = Name.ToArray()
        'displaying array values
        For Each element In array
            Dim value As String = element
            Console.WriteLine(value)
        Next
        Console.Read()
    End Sub

End Module

OutPut


 Interview Question
Difference between Array and Arraylist? view Answer

No comments:

Post a Comment