Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

09 Arrays


'Arrays are variables which hold multiple values.  Each of the values is indexed.

'Declare an array with Dim
'Arrays default to a lower bound of zero but the user must specify the upper bound

Dim MyArray(5) As Long   '6 elements, 0-6

'Users can define the upper/lower bounds
Dim MyArray(1 To 15)
Dim MyArray( - 10 To 10)

'Arrays can have multiple dimensions
Dim MyArray(5,5)
Dim MyArray(1 To 10, 1 To 50)

'Arrays whose dimensions are set with Dim are fixed-size arrays.  The dimensions
'cannot be changed.

'Arrays whose dimensions can be changed are called dynamic arrays.
'They are declared with ReDim and later dimensioned again, as often as needed
Redim MyArray()   'dynamic array

'then later in code:
Redim MyArray(15)   'the dimensions can be changed at any time to any value

'ReDim loses values in the array unless Preserve is used
Redim Preserve MyArray(15)

'Control arrays
Variables can contain objects, such As textboxes Or pictureboxes, And VB supports
arrays of objects As well.