VBScript:如何利用函数返回的字典对象?

时间:2022-07-16 07:31:37

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?

如何调用此函数并使用返回的字典?

EDIT: I've tried this:

编辑:我试过这个:

Dim someStuff
someStuff = GetSomeStuff

and

Dim someStuff
Set someStuff = GetSomeStuff

When I try to access someStuff, I get an error:

当我尝试访问someStuff时,出现错误:

Microsoft VBScript runtime error: Object required: 'GetSomeStuff'

EDIT 2: Trying this in the function:

编辑2:在函数中尝试这个:

Set GetSomeStuff = stuff

Results in this error:

导致此错误:

Microsoft VBScript runtime error: Wrong number of arguments or invalid property assignment.

3 个解决方案

#1


22  

I wasn't too sure of what was your problem, so I experimented a bit.

我不太确定你的问题是什么,所以我试验了一下。

It appears that you just missed that to assign a reference to an object, you have to use set, even for a return value:

您似乎错过了为对象分配引用,您必须使用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

#2


4  

Have you tried doing
set GetSomeStuff = stuff
in the last line of the function?

你有没有尝试在函数的最后一行设置GetSomeStuff = stuff?

#3


0  

Have you tried:

你有没有尝试过:

Dim returnedStuff
Set returnedStuff = GetSomeStuff()

Then "For Each" iterating over the dictionary? There's an example of using the Dictionary (albeit for VB6, the gist of it is the same though!) here.

然后“For Each”迭代字典?有一个使用字典的例子(虽然对于VB6,但它的要点是相同的!)。

#1


22  

I wasn't too sure of what was your problem, so I experimented a bit.

我不太确定你的问题是什么,所以我试验了一下。

It appears that you just missed that to assign a reference to an object, you have to use set, even for a return value:

您似乎错过了为对象分配引用,您必须使用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

#2


4  

Have you tried doing
set GetSomeStuff = stuff
in the last line of the function?

你有没有尝试在函数的最后一行设置GetSomeStuff = stuff?

#3


0  

Have you tried:

你有没有尝试过:

Dim returnedStuff
Set returnedStuff = GetSomeStuff()

Then "For Each" iterating over the dictionary? There's an example of using the Dictionary (albeit for VB6, the gist of it is the same though!) here.

然后“For Each”迭代字典?有一个使用字典的例子(虽然对于VB6,但它的要点是相同的!)。