Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing two-dimensional array through functions

Tags:

vb.net

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?

like image 472
Wine Too Avatar asked Dec 27 '22 10:12

Wine Too


1 Answers

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.

like image 94
SysDragon Avatar answered Jan 08 '23 07:01

SysDragon