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
 
 
 
 
 
TitleIterate over the items in an array of unknown dimension
Keywordsiterate, array, dimension, multi-dimensional, unknown, For Each, loop
CategoriesSyntax
 
Use For Each.

The following code initializes a three-dimensional array where each dimension has different bounds. It then uses a For Each loop to iterate over all of the items in the array. Note that the code cannot tell which item is in which position in the array.

 
Private Sub Form_Load()
Dim arr() As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim v As Variant

    ' Initialize the array.
    ReDim arr(1 To 2, 10 To 12, 69 To 71)
    For i = LBound(arr, 1) To UBound(arr, 1)
        For j = LBound(arr, 2) To UBound(arr, 2)
            For k = LBound(arr, 3) To UBound(arr, 3)
                arr(i, j, k) = i & ", " & j & ", " & k
            Next k
        Next j
    Next i

    ' Iterate over the items in the array using For Each.
    For Each v In arr
        Debug.Print v
    Next v
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated