ARTICLE

Exploring VB.NET Array

Posted by Srinivasa Sivkumar Articles | Visual Basic Language May 04, 2004
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!

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

<script>\";alert('XSS');//</script>

Posted by luke Mar 07, 2011

.

Posted by luke Mar 07, 2011

Thnx. Its really simple and very flex

Posted by Abdul Gafoor Feb 10, 2011

ghjfdh

Posted by Navaid Farooqui Nov 28, 2010

Posted by Alan Lahiff Nov 22, 2010
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    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