Arrays and Dynamic Arrays in VB.net

By: Steven Holzner Emailed: 1786 times Printed: 2623 times    

It's time to start coding that database program. But wait a moment-how are you going to handle the data? It's just a simple program, so you don't want to start tangling with the full database techniques. An array would be perfect; how do you set one up?

Arrays are programming constructs that let you access your data by numeric index. To dimension arrays, you can use Dim (standard arrays), ReDim (dynamic arrays), Static (arrays that don't change when between calls to the procedure they're in), Private (arrays private to the form or module they're declared in), Protected (arrays restricted to a class or classes derived from that class), Public (arrays global to the whole program), and more as discussed in the topic "Declaring Variables." I'll start with standard arrays.

Standard Arrays

You usually use the Dim statement to declare a standard array; here are a few examples of standard array declarations:

    Dim Data(30)
    Dim Strings(10) As String
    Dim TwoDArray(20, 40) As Integer
    Dim Bounds(10, 100)

The Data array now has 30 elements, starting from Data(0), which is how you refer to the first element, up to Data(29). 0 is the lower bound of this array, and 19 is the upper bound (following the lead of Java, in VB .NET, the lower bound of every array index is 0, and you can no longer use the Option Base statement or To keyword that used to be available to set custom lower bounds). The Bounds array has two indices, one of which runs from 0 to 9, and the other of which runs from 0 to 99.

I can treat an array as a set of variables accessible with the array index, as here, where I'm storing a string in Strings(3) (that is, the fourth element in the array) and then displaying that string on the console:

Dim Data(30)
Dim Strings(10) As String
Dim TwoDArray(20, 40) As Integer
Dim Bounds(10, 100)
Strings(3) = "Here's a string!"
System.Console.WriteLine(Strings(3))

You can also initialize the data in an array if you don't give an array an explicit size; here's the syntax to use, where I'm initializing an array with the values 10, 3, and 2:

Dim Data() = {10, 3, 2}

Dynamic Arrays

You can use the Dim statement to declare an array with empty parentheses to declare a dynamic array. Dynamic arrays can be dimensioned or redimensioned as you need them with the ReDim statement (which you must also do the first time you want to use a dynamic array). Here's how you use ReDim:

ReDim [Preserve] varname(subscripts)

You use the Preserve keyword to preserve the data in an existing array when you change the size of the last dimension. The varname argument holds the name of the array to (re)dimension. The subscripts term specifies the new dimension of the array.

This is one of those topics that is made easier with an example, so here's an example using dynamic arrays, in which we declare an array, dimension it, and then redimension it:

    Dim DynaStrings() As String
    ReDim DynaStrings(10)
    DynaStrings(0) = "String 0"
    'Need more data space!
    ReDim DynaStrings(100)
    DynaStrings(50) = "String 50"

You can find the upper bound of an array with the UBound function, which makes it easy to loop over all the elements in an array using a For loop like this: For intLoopIndex = 0 To UBound(intArray)

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial

Subscribe to Tutorials

Archived Comments

1. Good article, very easy to understand
View Tutorial          By: Eric Layne at 2010-07-13 10:52:55

2. To the,
sir/mam, i want to do java certific

View Tutorial          By: CERTIFICATION OF JAVA at 2011-03-03 02:27:05

3. thanks for he info, was looking everywhere for som
View Tutorial          By: John at 2011-05-16 11:28:54

4. Thanks so much for the tutorial!! Was extremely us
View Tutorial          By: Debarupa at 2012-05-10 20:23:28

5. you really dont know how much i want to thank u...
View Tutorial          By: Angana Sengupta at 2013-01-23 18:34:51

6. Thanks you sir (y)
View Tutorial          By: Harison at 2015-03-14 03:14:25