Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
-->
TitleDeclare arrays in VB .NET
Keywordsvariable, VB .NET, initialization, declaration, array
CategoriesVB.NET
 
The following code shows how to declare an array in VB .NET. The value 10 gives the upper bound for the array. The lower bound is always 0 so this array contains 11 elements numbered 0 through 10.
 
Dim values(10) As Integer
 
As in VB 6, you can declare an array without bounds it. Later you can use ReDim to give it a size.
 
Dim values() As Integer
...
ReDim Preserve values(5)
 
Declare multi-dimensional arrays by separating the dimensions with commas.
 
Dim values1(9, 9) As Integer ' A 100 element array.

Dim values2(,) As Integer    ' No bounds yet.
ReDim values2(9, 9)          ' Give it bounds.
 
If you declare an array without bounds, you can initialize it during the declaration. Put the array items inside parentheses, separated with commas. The system automatically figures out what dimensions to use.
 
' An array with three values,
' indexes 0 through 2.
Dim values() As Integer = {1, 2, 3}
 
To initialize an array of objects, use the object constructors inside the value vector.
 
Dim primary_colors() As Pen = { _
    New Pen(Color.Red), _
    New Pen(Color.Green), _
    New Pen(Color.Blue) _
}
 
For multi-dimensional arrays, put values for an array of one fewer dimensions inside more parentheses and separated by commas.
 
' A 2-D array with six values,
' indexes (0, 0) through (1, 2).
Dim values(,) As Integer = { _
    {1, 2, 3}, _
    {4, 5, 6}}

' A 3-D array with 12 values,
' indexes (0, 0, 0) through (1, 1, 2).
Dim values(,) As Integer = { _
    {{1, 2, 3}, _
     {4, 5, 6}}, _
    {{7, 8, 9}, _
     {10, 11, 12}} _
}
 

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