Multidimensional Arrays using an Enumerator in VB.NET
In this article I will explain you about Multidimensional Arrays using an Enumerator in VB.NET.
As you learn in my previous article, An array in VB.NET is simply a set of sequential memory locations that can be accessed using either indices or references. Now we talk about multidimensional array in VB.NET.
Multidimensional Arrays: A multidimensional array of dimension n is a collection of items accessed via n subscript expressions. It can be declared with a different notation but in the same way as single-dimensional arrays, as shown in Listing 20.2.
Listing 20.2: Creating and Assigning values to Multidimensional Arrays
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Namespace array
Class Program
Shared Sub Main(ByVal args As String())
' simply like below
' 3*2 member two-dimensional arrays
Dim intCount As Integer(,) = New Integer(,) {{1, 2, 3}, {4, 5, 6}}
' 1*3 member three-dimesional arrays
Dim intDec As Integer(,,) = New Integer(9, 19, 29) {}
'or
' Create and initialize a new three-dimensional array instance of type integer
Dim myArr As System.Array
myArr = System.Array.CreateInstance(GetType(Integer), 2, 3, 4)
For i As Integer = myArr.GetLowerBound(0) To myArr.GetUpperBound(0)
For j As Integer = myArr.GetLowerBound(1) To myArr.GetUpperBound(1)
For k As Integer = myArr.GetLowerBound(2) To myArr.GetUpperBound(2)
myArr.SetValue((i * i) + (j * j) + k, i, j, k)
Next
Next
Next
End Sub
End Class
End Namespace
Listing 20.3 provides another example of multidimensional arrays that incorporates an enumerator class.
Listing 20.3: Multidimensional Arrays using an Enumerator
// array example - multidimensional
Public Class ArrayMembers
Public Shared Sub Main()
Dim myIntArray As Integer(,) = New Integer(,) {{1, 2, 3}, {4, 5, 6}}
Dim i As Integer = 0
Console.WriteLine("myIntArray.GetUpperBound(0):" & myIntArray.GetUpperBound(0))
Console.WriteLine("myIntArray.GetUpperBound(1):" & myIntArray.GetUpperBound(1))
Dim myEnumerator As System.Collections.IEnumerator = myIntArray.GetEnumerator()
Dim cols As Integer = myIntArray.GetLength(myIntArray.Rank - 1)
While myEnumerator.MoveNext()
If i < cols Then
i += 1
Else
Console.WriteLine()
i = 1
End If
Console.Write(vbTab & "{0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class
Output Window
Conclusion
Hope this article would have helped you in understanding Multidimensional Arrays using an Enumerator in VB.NET.