What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDeclare array objects in VB .NET
Keywordsvariable, VB .NET, initialization, declaration, array objects
Okay, I fibbed a little when I said in How to declare arrays in VB .NET that all arrays have 0 as their lower bounds. There is actually an Array class and it can have non-zero lower bounds. Using it is not as easy as using normally declared arrays, however.

The Array class does not have its own constructor so you cannot make a pure Array object from scratch. However, the Array class has a shared method named CreateInstance that lets you make arrays of specific types. There are several overloaded versions of the CreateInstance method. For example, the following code creates a three-dimensional array of Strings. The first dimension has three items, the second has three, and the third has ten. The array contains 90 elements with indexes running from (0, 0, 0) to (2, 2, 9).

Dim names As Array = Array.CreateInstance( _
    GetType(String), 3, 3, 10)
"Big deal," I hear you say. You could have done this more simply using the following code.
Dim names(2, 2, 9) As String
Most of the overloaded versions of CreateInstance are just this uninteresting. One, however, takes as parameters a data type, an array of dimension lengths, and an array of lower bounds for the dimensions. The following code creates another 90 element String array. This time the indexes run from (1, -10, 10) to (3, -8, 19). Note how the code declares the arrays of lengths and lower bounds inside the call to CreateInstance without creating separate varaibles for those arrays.
Dim names As Array = Array.CreateInstance( _
    GetType(String), _
    New Integer() {3, 3, 10}, _
    New Integer() {1, -10, 10} _

Related topics:
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.