ARTICLE

Exploring VB.NET Arrays

Posted by Srinivasa Sivkumar Articles | Visual Basic 2010 April 21, 2003
There are few differences between VB6 and VB.NET arrays. This article explains how VB.NET arrays differ from the VB6 arrays.
 
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.

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.

Login to add your contents and source code to this article
share this article :
post comment
 

Very very helpful for me. Please write more if you have further findings. Thank you .......

Posted by May Maung Apr 27, 2009

I want an equivalent for UBound in VB.Net. Dim arr as String arr="123456" Dim brr as String brr=Ubound(arr,2) I want to convert above in VB.Net

Posted by rahul nijhawan Mar 10, 2008

In this article we initialize the value at design time. Can u explain how to store values in array variables at runtime. I get value from text box and stored in variable then clear the text box, next value get from same text box stored in next index of the array variable. I try in two dimension array. I get value from text box and stored in (0,0) after i clear the text, no values can be stored after that. Let me know any example it will great help to me.

Posted by karthick babu Dec 05, 2007

Dear sir,

Please explain , How to add  a picture file in an Access database as well as retrieving also.

Using Vb.Net

Thank You.

Posted by Saba bathi Nov 29, 2006
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor