You want to use the ArrayList type in the VB.NET programming language. Although an array type may offer superior performance, the ArrayList can be expanded automatically by built-in code, making a dynamic array. Here, we describe the ArrayList type in the VB.NET language, which is one of the easiest and more useful data types.

First, the Add method on the VB.NET ArrayList instance type is very commonly used. The Add method appends the object argument to the very end of the internal ArrayList data structure. You do not need to check to see if there is room before adding the element; there will always be room except in extreme circumstances such as out-of-memory situations. This program adds three elements to the ArrayList.
Program that uses ArrayList and Add [VB.NET]
Module Module1
Sub Main()
' Create a new ArrayList.
' ... Then add three strings to it.
Dim list As New ArrayList
list.Add("One")
list.Add("Two")
list.Add("Three")
End Sub
End Module
Here, we describe how it is possible and often useful to receive an ArrayList type as a parameter to a Sub. This enables you to exploit structured programming with the ArrayList type to a larger extent. The Example method here could be used with any ArrayList instance, with any elements in its internal storage.
Program that uses ArrayList with method [VB.NET]
Module Module1
Sub Main()
' Create an ArrayList and add two elements to it.
Dim list As New ArrayList
list.Add(5)
list.Add(7)
' Use ArrayList as an argument to the method.
Example(list)
End Sub
''' <summary>
''' Receives ArrayList as argument.
''' </summary>
Private Sub Example(ByVal list As ArrayList)
Dim num As Integer
For Each num In list
Console.WriteLine(num)
Next
End Sub
End Module
Output
5
7It is possible to add a range of elements from one ArrayList onto the end of another ArrayList. To do this, please consider using the AddRange method. The AddRange method receives one argument, which is an ArrayList type that contains elements you want to add to the end of the instance ArrayList you call the method on. In this example, the two array lists are effectively concatenated.
Program that uses AddRange method [VB.NET]
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
Output
5
7
10
13Often when programming with the ArrayList type, you will not be sure how many elements are in the current instance. Fortunately, the ArrayList offers the Count instance property. This property quickly returns the number of elements in the ArrayList. Also, this example demonstrates the Clear method. After you call the Clear method, the Count property will return zero elements: this is because all of the elements were removed.
Program that uses ArrayList and Count property [VB.NET]
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
Output
2
0Let's demonstrate how you can use the Add, RemoveAt, Insert, and RemoveRange methods on the ArrayList type in the VB.NET programming language. We have already seen the Add method in the first example in this article. Next, we see how the RemoveAt method works: it receives an index argument, which corresponds to the element index you want to remove.
The Insert method receives two arguments: the position you want to insert at, and the object reference itself. Finally, the RemoveRange methods receives two arguments: the index you want to start removing at, and the number of elements you want to remove.
Program that uses Add, RemoveAt, Insert, RemoveRange [VB.NET]
Module Module1
Sub Main()
' Create an ArrayList and add three strings to it.
Dim list As New ArrayList
list.Add("Dot")
list.Add("Net")
list.Add("Perls")
' Remove a string.
list.RemoveAt(1)
' Insert a string.
list.Insert(0, "Carrot")
' Remove a range.
list.RemoveRange(0, 2)
' Display.
Dim str As String
For Each str In list
Console.WriteLine(str)
Next
End Sub
End Module
Output
Perls
In the ArrayList type, elements are not stored with a type directly; instead they are accessed through the object base type. To cast an object to the more derived type you want to use, please use the TryCast operator. The syntax for this operator receives two arguments: the element you want to cast from the ArrayList, and then the type to which you want to cast. The TryCast operator will not throw exceptions, as it uses the tester-doer pattern.
Program that uses ArrayList and TryCast [VB.NET]
Module Module1
Sub Main()
' Create a new ArrayList.
Dim list As New ArrayList
list.Add("man")
list.Add("woman")
list.Add("plant")
' Loop over the ArrayList with a For loop.
Dim i As Integer
For i = 0 To list.Count - 1
' Cast to a string.
Dim str As String = TryCast(list.Item(i), String)
Console.WriteLine(str)
Next i
End Sub
End Module
Output
man
woman
plantIn this example, we look at how you can programmatically extract one part of an ArrayList instance into another ArrayList instance. To do this, please use the GetRange instance method on the original ArrayList instance. Then, assign the result of the GetRange method call to a new ArrayList variable reference. The GetRange uses the standard pattern for range-based methods: it receives the starting index from which you want to copy, and then the count of elements you want to get.
Program that uses ArrayList and GetRange [VB.NET]
Module Module1
Sub Main()
' Create new ArrayList.
Dim list1 As New ArrayList
list1.Add("fish")
list1.Add("amphibian")
list1.Add("bird")
list1.Add("plant")
' 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
Output
bird
plant
We examined the ArrayList type as used in the VB.NET programming language. While the ArrayList type is a core class in the .NET Framework, its usage in VB.NET is somewhat nuanced due to the casting syntax in this high-level language. Although this article does not cover all aspects of the ArrayList, it provides a thorough tutorial for many common operations, including variations on adding, inserting, removing, and copying elements.
VB.NET Tutorials