ARTICLE

Exploring Array in VB.NET

Posted by Srinivasa Sivkumar Articles | Visual Basic .NET November 10, 2012
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).
Reader Level:

What's new in VB.NET Arrays? 

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. See the eWeek Article on this. 

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. 

Dim Ary(4) As Integer
Dim
 i As Integer
For
 i = 0 to Ary.GetUpperBound(0) 'Or UBound(Ary)
Ary(i) = i
Next 

The GetUpperBound method is equivalent to the UBound statement in VB6 (The GetUpperBound method is accessible by all the .NET-enabled languages). Still the UBound statement works in VB.NET. 

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. 

Dim Ary(0 to 4) as Integer

But in VB.NET we can mimic the same declaration like this,

Dim Ary(4) As Integer
OR 
Dim Ary As Integer = New Integer(4) {}

We can even have a default value for arrays like this,

Dim Ary As Integer = New Integer(4) {0, 1, 2, 3}
OR 
Dim Ary(4) As Integer = {0, 1, 2, 3}

We can change the above VB.NET array with the ReDim statement.

Dim Ary(4) As Integer
ReDim
 Ary(10) 
OR 
Dim Ary(4) As Integer
ReDim
 Preserve Ary(10)

VB.NET allows declaring multi dimensional arrays. For example, the following statement declares three-dimensional array in VB.NET.

 

Dim Ary(,,) As Integer

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. 

Dim ary() As Integer
If
 ary = Nothing then
Response.Write("The array is un-initialized")
Else
Response.Write("The array is initialized")
End if

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.

Until next time, Happy Programming!

COMMENT USING
Employers - Post Free Jobs