terça-feira, agosto 30, 2005

VB.NET Array usage

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}} _
}

http://www.vb-helper.com

1 comentário:

Wicked Web Programmers disse...
Este comentário foi removido por um gestor do blogue.