我可以将数组作为“ParamArray”传递吗?

时间:2014-11-02 23:10:24

标签: arrays vb.net

我尝试做的是传递一个未定义数量的多维数组(以创建矩阵乘法函数)。

这样的事情:

Sub main()

Dim A_out2 as Double(,)
Dim A_out3 as Double(,)
Dim A1 as Double(,) = {{1,2}, {3,4}}

A_out2 = matXmat(A1, A1)

A_out3 = matXmat(A1, A1, A1)

end sub


'I know this is wrong, but you get what I'm trying to do:

function matXmat(ByVal ParramArray args(,) as Double()) as Double(,)

'Number of matrices

dim numMat as double = args.length

'Matrix multiplication loop

end function

2 个答案:

答案 0 :(得分:1)

您的参数可以是锯齿状数组,例如:

ParamArray args As Double()(,)

使用它,我的测试基于您的示例编译。

您可以从参数中检索第一个2维数组,如下所示:

Dim test(,) As Double = args(0) 'etc.

你是在正确的轨道上,但你不能在类型后面和参数名后面都有你的数组说明符 - 它们只需要在一个位置(例如,ParamArray args As Double()(,)或者ParamArray args()(,)As Double)。

答案 1 :(得分:1)

在这里,我将更多地解释一下我在评论中给你的最后一个选择:

'For example:
Dim A1 As Double(,) = {{1, 2}, {3, 4}}
Dim A2 As Double(,) = {{5, 6}, {7, 8}}
Dim A3 As Double(,) = {{9, 0}, {1, 2}}

Dim result As Double(,) = matXmat(A1, A2)   ' result = A1 x A2

result = matXmat(result, A3)   ' result = A1 x A2 x A3

' If exists one more matrix then
'result = matXmat(result, A4)   ' result = A1 x A2 x A3 x A4