Dynamic Array in Visual Basic .NET
Last Updated: March 19, 2020
Can you run the following code?
Module Module1 Sub Main() Dim array(3) As Integer array(0) = 1 array(1) = 2 array(2) = 3 array(3) = 4 array(4) = 1 End Sub End Module
You will get the following error message
Index was outside the bounds of the array.
Why?
Size of the array is 4 but you are going to add the 5th item to the array
But you can run the following code
Module Module1 Sub Main() Dim array(3) As Integer array(0) = 1 array(1) = 2 array(2) = 3 array(3) = 4 ReDim array(4) array(4) = 1 Console.ReadLine() End Sub End Module
@ Line 10 , I have used
ReDim array(4)
You can make the array dynamic using ReDim command