4

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?

2
  • In VB.NET try to get rid of your multidimensional/jagged arrays. In most cases you can replace them with something more readable,less error-prone and more expandable like a List(Of CustomClass). Commented Jan 24, 2013 at 12:12
  • Thank's Tim. I will consider that as soon as possible. Commented Jan 24, 2013 at 12:33

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.