How To Pass MultiDimensional Array?

BumblebeeTuna

Newcomer
Joined
Oct 5, 2006
Messages
1
Hello everyone, could someone please show me how to pass a multidimensional array of a varying size to a class that takes an multidimensional of a varying size. Here is some sample code that I have to better explain.

Inside the class I have this code

Visual Basic:
Public Class MapClass

    Public Sub New(ByVal Array()() As Short)
        MyBase.New()

        'Set The Array Passed In As The Map Array
        MapArray = Array

    End Sub


    Public MapArray()() As Short
 
    Public Sub Check()
        MessageBox.Show(MapArray(1)(2).ToString)
    End Sub

End Class

Now inside the main form I have this code
Visual Basic:
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       'Size Of The Map
        Dim MyArray(10, 10) As Short

        MyArray(1, 1) = 3                
        MyArray(1, 2) = 4
        MyArray(1, 3) = 5
        
        Dim MapItem As New MapClass(MyArray)
        MapItem.Check()

    End Sub

Inside the editor, it has MyArray underlined and it says
Value of type '2-dimensional array of Short' cannot be converted to '1-dimensional array of 1-dimensional array of Short' because 'Short' is not derived from '1-dimensional array of Short'.
 
You are using two different types of array as the error message points out to you.
Visual Basic:
' This code creates a 1 dimensional array of 1 dimentional arrays
Public MapArray()() As Short
' you would get the first item of this using
MapArray(0)(0)
' 
' Wheras this code creates a 2 dimensional array
Dim MyArray(10, 10) As Short
' you would get the first item of this using
MyArray(0, 0)
You problem solves itself if you stick to using one style or the other.
 
Back
Top