Reputation: 4655
I have interesting situation with passing two-dimensional array through functions.
Array is declared at form's level scope:
I try to rewrite a part of my code from VB6 where I have workable example.
Dim myArray(,) As Double
Then I get a sub where array is redimed and filled according to 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 array from other sub:
mySub(myArray)
Debug.Print(myArray(1, 5))
And here I get error message:
Object reference not set to an instance of an object.
Data in mySub is filled properly but I can't see this data in calling sub.
What do I do wrong and how can I get this scenario working?
Upvotes: 4
Views: 16934
Reputation: 9888
You can solve it by doing this:
Public Sub mySub(ByRef myArray(,) As Double)
'...
End Sub
You need to reference the variable in order to have the changes outside the Sub.
Upvotes: 11