Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the use of the dynamic array
By Nidhi Last Updated : November 14, 2024
Create a dynamic array
Here, we will create an array and then redefine the size of the array using the "Redim" keyboard and assign more elements to the array.
Program/Source Code:
The source code to demonstrate the use of a dynamic array is given below. The given program is compiled and executed successfully.
VB.Net code to create a dynamic array
'VB.Net program to demonstrate the use of the dynamic array.
Module Module1
Sub Main()
Dim intArray() As Integer
ReDim intArray(5)
intArray(0) = 10
intArray(1) = 20
intArray(2) = 30
intArray(3) = 40
intArray(4) = 50
ReDim Preserve intArray(10)
intArray(5) = 60
intArray(6) = 70
intArray(7) = 80
intArray(8) = 90
intArray(9) = 100
Console.WriteLine("Array elements are: ")
For i = 0 To 9
Console.Write("{0} ", intArray(i))
Next i
Console.WriteLine()
End Sub
End Module
Output
Array elements are:
10 20 30 40 50 60 70 80 90 100
Press any key to continue . . . Array elements are:
10 20 30 40 50 60 70 80 90 100
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains a function Main(). In the Main(), we created an array intArr of 5 elements and then we assigned the 5 elements to the array. After that, we redefined the size of the array using the ReDim and Preserve keyboard and then assigned some more elements to the array, and then print all elements of the array on the console screen.
VB.Net Array Programs »