Single Dimension
vb
Dim TheArray(9) As Integer
This defines a fixed size array of Integers containing 10 elements. The first one is element Zero and the last element Nine
The elements can accessed
vb
For i As Integer=0 To 9
TheArray(i)=i
Next i
It is also possible to define an array which can be resized
vb
Dim TheArray() As Integer
To create an array containing 10 elements.
vb
ReDim TheArray(9)
For i As Integer=0 To 9
TheArray(i)=i
Next i
Note using ReDim this way will clear all of the contains previously stored in the array.
Now suppose we want to resize the array to contain 20 items
without clearing the contents
vb
ReDim Preserve TheArray(20)
Two DimensionImagine you want to recreate the multiplication table.
vb
Dim TheArray(9,9) As Integer
For x As Integer=0 To 9
For y As Integer=0 To 9
TheArray(x,y)=x*y
Next y
Next x
It is also possible to use a resizable array.
vb
Dim TheArray(,)
ReDim TheArray(9,9)
For x As Integer=0 To 9
For y As Integer=0 To 9
TheArray(x,y)=x*y
Next y
Next x
Three DimensionsA Fixed Size array.
vb
Dim TheArray(9,9,9) As Integer
For x As Integer=0 To 9
For y As Integer=0 To 9
For z As Integer=0 To 9
TheArray(x,y)=x*y*z
Next z
Next y
Next x
It is also possible to use a resizable array.
vb
Dim TheArray(,,)
ReDim TheArray(9,9,9)
For x As Integer=0 To 9
For y As Integer=0 To 9
For z As Integer=0 To 9
TheArray(x,y)=x*y*z
Next z
Next y
Next x
Note If you use ReDim with
Preserve on arrays with multiple dimenstions.
You can only resize the right-most dimension,
vb
ReDim Preserve TheArray(1,2,3)
Higher DimensionYou can extend the number up to any number provided the array fits in memory.