如何在VB.NET中有任意大小的函数输入?

时间:2015-01-29 16:55:18

标签: vb.net

我找到了C ++的答案:How to make a function receive multidimentional array of arbitrary size in C++?

但是我无法将其翻译成VB.NET。

我可以将不同的维度折叠成一个矢量数组......但出于好奇,有没有办法让函数在VB.NET中采用任意大小的双倍?

下面是一些代码,以便您了解我正在尝试做什么:

Dim x(), x1(,), x2(,,) as Double

manipulate_in_matlab(x)

manipulate_in_matlab(x1)

manipulate_in_matlab(x2)

function manipulate_in_matlab(ByVal x as ???)
'This function calls in another program (Matlab in this case)

     Dim x_size as Integer = x.Rank

     'I'm not sure how to proceed here, right now the 'dumb' way would be a case statement. But what I need to do is create an array of equal size. For the 3-dimension case:

     Dim r, c, d As Double
     r = x.GetUpperBound(0)
     c = x.GetUpperBound(1)
     d = x.GetUpperBound(2)

     Dim x_imag(r,c,d) as Double


end function

1 个答案:

答案 0 :(得分:0)

致电代码:

Dim a(5) As Double
Dim a1(5, 5) As Double
Dim a2(5, 5, 5) As Double
test(a, a1, a2)

功能定义

Private Sub test(ByVal a() As Double, ByVal a1(,) As Double, ByVal a2(,,) As Double)

End Sub

如果您知道只有某些组合,则使用重载

Dim a(5) As Double
Dim a1(5, 5) As Double
Dim a2(5, 5, 5) As Double

test(a)
test(a1)
test(a2)


 Private Sub test(ByVal a() As Double)

    End Sub

    Private Sub test(ByVal a(,) As Double)

    End Sub

    Private Sub test(ByVal a(,,) As Double)

    End Sub

编辑3:抱歉,我一直在想不同的事情

致电代码:

test1(a)
test1(a1)
test1(a2)

功能定义:

Private Sub test1(ByVal a As Array)
    Debug.WriteLine(a.Rank)

End Sub