我在以下子test()上收到“按引用参数类型不匹配”错误:
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
当我GetOtherDict
用curKey
参数而不是参数调用函数时,k
错误消失了。
您能告诉我为什么需要这个多余的声明吗?
另外,您已经k As String
在函数中声明了该函数,因此该函数希望您将a传递String
给它。由于您尚未k
在中声明Sub Test()
,因此k
将被视为a Variant
,因此您通过引用参数类型不匹配获得了该“错误”。
当你通过它不会给你一个错误curKey
,因为curKey
被定义为String
在Sub Test()
其中有什么功能预计...
另一个提示:请Option Explicit
在代码末尾使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句