VB:将2D数组传递给Sub

时间:2016-11-14 14:06:30

标签: arrays vb.net function

我有一个二维数组,其()内的大小不是静态的。 Aka代码将在稍后分配大小。我想将此数组传递给我的函数,以便我可以使用它执行大量代码。

我的阵列:

'generate Ingredient array
Dim Ingredients(checkCount2 - 1, 3) As String
Dim maxDim0 As Integer = UBound(Ingredients, 1)
Dim maxDim1 As Integer = UBound(Ingredients, 2)

我尝试了以下内容:

 For i As Integer = 0 To maxDim0
     For j As Integer = 0 To maxDim1
        GenerateNewImport(Ingredients(i, j), F_IDCode, F_Description, F_VersionDate, F_VersionNumber)
     Next j
 Next i

OR

GenerateNewImport(Ingredients, F_IDCode, F_Description, F_VersionDate, F_VersionNumber)

OR

GenerateNewImport(Ingredients(), F_IDCode, F_Description, F_VersionDate, F_VersionNumber)

OR

GenerateNewImport(Ingredients(checkCount2-1, 3), F_IDCode, F_Description, F_VersionDate, F_VersionNumber)

等,但我一直收到错误:

(Ingredients, ... :   Value of type '2-dimensional array of String' cannot be converted to '1-dimension

我的功能是:

Sub GenerateNewImport(ByVal Ingredients(,) As String, ByVal F_IDCode As String, ByVal F_Description As String, ByVal F_VersionDate As String, ByVal F_VersionNumber As String)

End Sub

有关如何将数组传递给函数调用的任何建议吗?

以下引用以及其他许多内容:

https://msdn.microsoft.com/en-us/library/5dk93f6e(v=vs.90).aspx

2 个答案:

答案 0 :(得分:1)

ByVal Ingredients(,) As String

必须替换为

ByRef Ingredients As String(,)

编辑:我用 ByRef

替换了关键字 ByVal

答案 1 :(得分:1)

你说你试过打电话

Sub GenerateNewImport(ByVal Ingredients(,) As String, ByVal F_IDCode As String, ByVal F_Description As String, ByVal F_VersionDate As String, ByVal F_VersionNumber As String)

End Sub

使用此代码

GenerateNewImport(Ingredients, F_IDCode, F_Description, F_VersionDate, F_VersionNumber)

但这种组合实际上是正确的。接受的答案必须处理将签名从Ingredients(,) As String更改为Ingredients As String(,)并使用ByRef,因为它是一个数组。但是,无论如何都将数组作为引用类型传递,因此这是无关紧要的。在任何一种情况下,(,)的位置都是相同的

这是一个简单的例子,展示了如何将多维数组传递给方法,并且首先没有问题。

Option Explicit On
Option Strict On

Module Module1

    Sub Main()
        Dim ingredients(1, 1) As String
        ingredients(0, 0) = "a"
        ingredients(0, 1) = "b"
        ingredients(1, 0) = "c"
        ingredients(1, 1) = "d"
        Signature1(ingredients)
        Signature2(ingredients)
        Signature3(ingredients)
        Signature4(ingredients)
        For i As Integer = ingredients.GetLowerBound(0) To ingredients.GetUpperBound(0)
            For j As Integer = ingredients.GetLowerBound(1) To ingredients.GetUpperBound(1)
                Console.WriteLine(ingredients(i, j))
            Next
        Next
        Console.ReadLine()
    End Sub

    Sub Signature1(ByVal ingredients As String(,))
        ingredients(0, 0) = UCase(ingredients(0, 0))
        Console.WriteLine("You called Signature1")
    End Sub

    Sub Signature2(ByVal ingredients(,) As String)
        ingredients(0, 1) = UCase(ingredients(0, 1))
        Console.WriteLine("You called Signature2")
    End Sub

    Sub Signature3(ByRef ingredients As String(,))
        ingredients(1, 0) = UCase(ingredients(1, 0))
        Console.WriteLine("You called Signature3")
    End Sub

    Sub Signature4(ByRef ingredients(,) As String)
        ingredients(1, 1) = UCase(ingredients(1, 1))
        Console.WriteLine("You called Signature4")
    End Sub

End Module

输出显示所有签名都是等效的,并且全部通过ByRef

  

你打电话给Signature1   你叫Signature2   你叫Signature3   你叫Signature4   
一   
乙   
ç   
d