ARTICLE

Multidimensional Arrays using an Enumerator in VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language October 04, 2010
In this article I will explain you about Multidimensional Arrays using an Enumerator in VB.NET.
 
Reader Level:

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

multi-array.gif

Conclusion

Hope this article would have helped you in understanding Multidimensional Arrays using an Enumerator in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor