通过函数传递二维数组 [英] Passing two-dimensional array through functions

查看:38
本文介绍了通过函数传递二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过函数传递二维数组时遇到了有趣的情况.
数组在表单级别范围内声明:
我尝试从 VB6 重写我的部分代码,其中有可行的示例.

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.

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

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?

推荐答案

你可以这样做:

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

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

You need to reference the variable in order to have the changes outside the Sub.

这篇关于通过函数传递二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆