Programming Tutorials

Arrays and Dynamic Arrays in VB.net

By: Steven Holzner in VB.net Tutorials on 2008-11-25  

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)-






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)