What's New in VB - Net Arrays? There Are
What's New in VB - Net Arrays? There Are
There are few differences between VB6 and VB.NET arrays. Let's find out how
VB.NET arrays differ from the VB6 arrays. The main difference between VB.NET and
VB6 based arrays are VB.NET arrays always starts from the element zero (Arrays
in .NET enabled languages are inherited from the System.Array class). We have the
option of setting the array index with "Option Base" statement in VB6. Option Base
statement is obsolete in VB.NET (This is going to be a major problem when we are
porting out code from VB6 to VB.NET. This change is critically required to give
interoperability between other .NET-enabled languages). The next major difference
was, when we declare an array in VB.NET with 4 elements it'll have only 4 elements
unlike vb6, which will have 5 elements. This was only with Beta1 and Microsoft
revert the changes in Beta2 to be consistent with VB6 arrays.
In VB6 we'll get 5 elements raging from 0 to 4 in the array (Assuming we are not
overriding the lower boundary of the array to 1 by the Option Base statement). But
in the VB.NET we'll only get 4 elements raging from 0 to 3. If we try to access the
4th element we'll get the "IndexOutOfRangeException". So when accessing the
VB.NET array always lower one element from the upper bound of the array like this.
In VB.NET there is no concept of fixed length arrays. For example we can declare a
fixed length array in VB6, which can't be changed by the ReDim statement.
OR
OR
OR
VB.NET allows declaring multi dimensional arrays. For example, the following
statement declares three-dimensional array in VB.NET;
Then we can use the ReDim statement to declare the size of the array like this.
ReDim Ary(5, 5, 5)
Dim x, y, z As Integer
For x = 0 To 4
For y = 0 To 4
For z = 0 To 4
Ary(x, y, z) = x + y + z
Next
Next
Next
We all should know the one limitation of the ReDim statement. The ReDim statement
can't dynamically change the number of dimensions the array has. For example if
we've a three-dimensional array we can redim it in to a four or two-dimensional
array. All we can do is, we can change the lost dimension array of the array with the
ReDim statement.
In VB.NET we can compare an array with the keyword nothing to make sure the
array is un-initialized, which is not possible with VB6.
Summary.
I this article, I've pointed out few key differences between VB6 and VB.NET arrays. I
hope you'll find the article helpful and interested.
A
Dim arrNumbers(4) As Integer 'Declares & initialises an array of 5
integers, with indexes ranging from 0 to 4
B
Dim arrNumbers() As Integer 'Declares the array of integers
arrNumbers = New Integer() {0,1,2,3,4} 'Initialises the array to five
members & sets their values
C
Dim arrNumbers(32) As Integer' Declares & Initialises an array of
integers
ReDim arrNumbers(45) As Integer' Re-initialises the array
D
Dim arrNumbers () As Integer = {0,1,2,3,4}' Declares & initialises the
array
ReDim Preserve arrNumbers (25) 'Resizes the array, but retains the data
in elements 0 through 4