VBScript:如何利用从函数返回的字典对象?
时间:2020-03-06 14:47:20 来源:igfitidea点击:
我正在尝试从函数返回字典。我相信该功能可以正常运行,但是我不确定如何利用返回的字典。
这是我功能的相关部分:
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
和
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.
解决方案
你有没有尝试过:
Dim returnedStuff Set returnedStuff = GetSomeStuff()
然后" For Each"遍历字典吗?这里有一个使用字典的示例(尽管对于VB6,其要旨是相同的!)。
你尝试过吗
`
设置GetSomeStuff =东西
在函数的最后一行?
我不太确定问题是什么,所以我做了一些实验。
似乎我们只是错过了为对象分配引用的过程,甚至必须为返回值使用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

