4

我有两个数组,我试图提取其中的公共值,函数 findUniques 应该返回连接并用逗号分隔的字符串(它们是名称)。我想我很接近,但我找不到问题所在。第 10 行出现错误

Function findUniques(astrArray1() As String, astrArray2() As String) As String

    Dim blnMP5 As Boolean
    blnMP5 = False
    Dim counter1 As Long
    Dim counter2 As Long

    For counter1 = LBound(astrArray1) To UBound(astrArray1)
        For counter2 = LBound(astrArray1) To UBound(astrArray2)
            If astrArray1(counter1) = astrArray2(counter2) Then
                blnMP5 = False
                If blnMP5 = True Then
                    findUniques() = findUniques & "," & "astrArray1()"
                End If
            End If
        Next counter2
    Next counter1

End Function
4

2 回答 2

4

您的错误在这一行:

findUniques() = findUniques & "," & "astrArray1()"

您正在尝试为函数调用分配一个值,这没有意义。

您应该在函数的开头声明一个变量并继续添加该变量并最终返回它:

Function findUniques(astrArray1() As String, astrArray2() As String) As String

    Dim blnMP5 As Boolean
    blnMP5 = False    
    Dim counter1 As Long
    Dim counter2 As Long  
    Dim uniquesString as String = ""

    For counter1 = LBound(astrArray1) To UBound(astrArray1)
        For counter2 = LBound(astrArray1) To UBound(astrArray2)
            If astrArray1(counter1) = astrArray2(counter2) Then
               blnMP5 = False
               If blnMP5 = True Then
                   uniquesString = uniquesString & "," & "astrArray1()"
               End If
             End If
        Next counter2
     Next counter1

End Function
于 2013-04-22T21:39:36.770 回答
0

声明一个变量来保存你正在构建的字符串

    Dim tempString as string

并在第 10 行添加

    tempString = tempString & "," &  astrArray1(counter1)

最后

    findUniques = tempString 
End function
于 2013-04-22T21:40:07.467 回答