VB add to array

An array is a collection of values of the same type. You can add more values or elements to the array after the keyword ReDim Preserve is used to extend the array.

Module Module1

   Sub Main()
     Dim x() As Integer = {1, 3, 4, 23, 22} ' five-element array
     For Each i In x ' print array elements
        Console.WriteLine(i)
     Next
     ReDim Preserve x(6) 'extend to seven-element array
     x(5) = 30 'add more values to the array
     x(6) = 25
     Console.WriteLine("........................")
     For Each i In x ' print array elements again
        Console.WriteLine(i)
     Next
    
     Console.Read()

   End Sub
End Module

If you need a more flexible way to work with a collection of values, the ArrayList class can meet your requirement. By using the ArrayList class, it easy to search or manipulate the elements of the collection.
Module Module1

   Sub Main()
     Dim ar() As Integer = {8, 33, 4}
     Dim al As New ArrayList()
     al.Add(90) 'add one item to the arraylist
     al.AddRange(ar) 'add many items to the arraylist
     al.Insert(3, 11) 'insert value 11 to third position in the arraylist
     For Each item In al ' print all items of the arraylist
        Console.WriteLine(item)
     Next
     Console.Read()

   End Sub
End Module

Posted by: Ly | post date: 02-18-2013 | Subject: VB.NET Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.