VBScript:如何利用从函数返回的字典对象? [英] VBScript: How to utiliize a dictionary object returned from a function?
问题描述
我正在尝试从函数返回字典.我相信该函数工作正常,但我不确定如何使用返回的字典.
I'm trying to return a dictionary from a function. I believe the function is working correctly, but I'm not sure how to utilize the returned dictionary.
这是我的函数的相关部分:
Here is the relevant part of my function:
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
如何调用这个函数并使用返回的字典?
How do I call this function and use the returned dictionary?
我试过这个:
Dim someStuff
someStuff = GetSomeStuff
和
Dim someStuff
Set someStuff = GetSomeStuff
当我尝试访问 someStuff 时,出现错误:
When I try to access someStuff, I get an error:
Microsoft VBScript runtime error: Object required: 'GetSomeStuff'
编辑 2:在函数中尝试这个:
EDIT 2: Trying this in the function:
Set GetSomeStuff = stuff
导致此错误:
Microsoft VBScript runtime error: Wrong number of arguments or invalid property assignment.
推荐答案
我不太确定你的问题是什么,所以我做了一些实验.
I wasn't too sure of what was your problem, so I experimented a bit.
看来您刚刚错过了为对象分配引用,您必须使用 set
,即使是返回值:
It appears that you just missed that to assign a reference to an object, you have to use set
, even for a return value:
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
这篇关于VBScript:如何利用从函数返回的字典对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!