Ciò può essere eseguito con cicli for innestati
Nota: Quando si utilizza un ciclo For Each per iterare elementi di un array, il segnaposto generato ad ogni iterazione è una copia del valore nella matrice reale. Le modifiche a quel valore non si rifletteranno nell'array originale. Se vuoi fare qualcosa di diverso da leggere le informazioni dovrai usare un ciclo For per indirizzare direttamente gli elementi dell'array.
Assumendo un array a due dimensioni, il seguente esempio di codice assegnerà un valore a ciascun elemento in ogni dimensione.
Dim MasterIndex(5, 2) As String
For iOuter As Integer = MasterIndex.GetLowerBound(0) To MasterIndex.GetUpperBound(0)
'iOuter represents the first dimension
For iInner As Integer = MasterIndex.GetLowerBound(1) To MasterIndex.GetUpperBound(1)
'iInner represents the second dimension
MasterIndex(iOuter, iInner) = "This Isn't Nothing" 'Set the value
Next 'iInner
'If you are only interested in the first element you don't need the inner loop
MasterIndex(iOuter, 0) = "This is the first element in the second dimension"
Next 'iOuter
'MasterIndex is now filled completely
Si potrebbe eventualmente utilizzare la proprietà .Rank
per scorrere dinamicamente su ogni dimensione
Se si vuole ciclare su una matrice irregolare come Konrad Rudolph è stato suggerendo (Questo funzionalmente più si avvicina implementazioni di array in altri più liberamente lingue digitati come PHP) si potrebbe andare a questo proposito in questo modo:
'This is a jagged array (array of arrays) populated with three arrays each with three elements
Dim JaggedIndex()() As String = {
New String() {"1", "2", "3"},
New String() {"1", "2", "3"},
New String() {"1", "2", "3"}
}
For Each aOuter As String() In JaggedIndex
'If you are only interested in the first element you don't need the inner for each loop
Dim sDesiredValue As String = aOuter(0) 'This is the first element in the inner array (second dimension)
For Each sElement As String In aOuter
Dim sCatch As String = sElement 'Assign the value of each element in the inner array to sCatch
sElement = "This Won't Stick" 'This will only hold value within the context of this loop iteration
Next 'sElement
Next 'aOuter
'JaggedIndex is still the same as when it was declared
ho aggiornato entrambi gli esempi per spiegare la richiesta più dettagliata – JoshHetland