vb.net 通过函数传递二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14501014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:00:47  来源:igfitidea点击:

Passing two-dimensional array through functions

vb.net

提问by Wine Too

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.

我有通过函数传递二维数组的有趣情况。
数组是在表单级别范围内声明的:
我尝试从 VB6 重写我的代码的一部分,在那里我有可行的示例。

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?

mySub 中的数据已正确填充,但在调用 sub 时我看不到这些数据。
我做错了什么,我怎样才能让这个场景发挥作用?

回答by SysDragon

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.

您需要引用该变量才能在 Sub 之外进行更改。