Passing a two-dimensional array through functions

I have an interesting situation with passing a two-dimensional array through functions.
The array is declared in the form level area:
I am trying to rewrite part of my code from VB6 where I have a workable example.

Dim myArray(,) As Double

      

Then I get a sub where the array is redrawn and populated according to the data, something like this, symbolic situation:

Public Sub mySub(ByVal myArray(,) As Double)

    Dim temparray() As Double = {3, 5, 7, 9}
    For a As Double = 0 temparray.length - 1
         ReDim Preserve myarray(2, temparray(a))
    Next a

    myArray(1, 5) = 3.14
    ... etc...
End Sub

      

And finally, I would like to fill and read data in an array from another sub:

mySub(myArray)
Debug.Print(myArray(1, 5))

      

And here I am getting the error:

An object reference is not set on an object instance.

The data in mySub is populating correctly, but I cannot see this data when I call sub.
What am I doing wrong and how can I get this script to work?

+3


source to share


1 answer


You can solve this by following these steps:

Public Sub mySub(ByRef myArray(,) As Double)
    '...
End Sub

      



You need to reference the variable to have changes outside of Sub.

+6


source







All Articles