2

我在以下子测试()中收到“按引用参数类型不匹配”错误:

Public Function GetOtherDict(k As String, dict As Dictionary) As Dictionary

    Dim otherDict As New Dictionary

    curItem = dict.Item(k)
    otherDict.Add curItem, curItem

    Set GetOtherDict = otherDict
End Function

Public Sub Test()

    Dim dict As New Dictionary
    dict.Add "a", 1
    dict.Add "b", 2

    For Each k In dict.Keys

        Dim otherDict As Dictionary
        Dim curKey As String
        curKey = k 
        Set otherDict = GetOtherDict(k, dict)

    Next

End Sub

当我GetOtherDictcurKey参数而不是参数调用函数时k,错误消失了。

你能告诉我为什么我需要这个多余的声明吗?

4

2 回答 2

3

此外,您已k As String在函数中声明,因此该函数希望您将 a 传递String给它。由于您没有在 中声明kSub Test()k被视为 a Variant,因此您会收到“ by ref argument type mismatch ”错误。

当你通过时它不会给你一个错误,curKey因为curKey它被定义为函数StringSub Test()期望的......

另一个提示:请Option Explicit在代码末尾使用。

于 2013-11-11T09:01:47.687 回答
-1

我很久没有编程 vba 但尝试

Public Function GetOtherDict(k As String, dict As Dictionary) As Dictionary

     Dim otherDict As New Dictionary

     curItem = dict.Item(hub)
     otherDict.Add curItem, curItem

     return otherDict
End Function
于 2013-11-11T09:05:42.930 回答