我正在尝试从函数返回字典。我相信该函数工作正常,但我不确定如何使用返回的字典。
这是我的功能的相关部分:
Function GetSomeStuff()
'
' Get a recordset...
'
Dim stuff
Set stuff = CreateObject("Scripting.Dictionary")
rs.MoveFirst
Do Until rs.EOF
stuff.Add rs.Fields("FieldA").Value, rs.Fields("FieldB").Value
rs.MoveNext
Loop
GetSomeStuff = stuff
End Function
如何调用这个函数并使用返回的字典?
编辑:我已经尝试过这个:
Dim someStuff
someStuff = GetSomeStuff
and
Dim someStuff
Set someStuff = GetSomeStuff
当我尝试访问 someStuff 时,出现错误:
Microsoft VBScript runtime error: Object required: 'GetSomeStuff'
编辑2:在函数中尝试这个:
Set GetSomeStuff = stuff
结果出现此错误:
Microsoft VBScript runtime error: Wrong number of arguments or invalid property assignment.
我不太确定你的问题是什么,所以我做了一些实验。
看来您错过了要分配对对象的引用,您必须使用set
,即使对于返回值:
Function GetSomeStuff
Dim stuff
Set stuff = CreateObject("Scripting.Dictionary")
stuff.Add "A", "Anaconda"
stuff.Add "B", "Boa"
stuff.Add "C", "Cobra"
Set GetSomeStuff = stuff
End Function
Set d = GetSomeStuff
Wscript.Echo d.Item("A")
Wscript.Echo d.Exists("B")
items = d.Items
For i = 0 To UBound(items)
Wscript.Echo items(i)
Next
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)