列举前两个元素

时间:2014-09-12 14:40:57

标签: list dictionary vbscript

我正在尝试以动态方式编写脚本,只采用列表中的前两个元素,我遇到了一些麻烦。以下是我列表中的内容细分

声明:

Set List = CreateObject("Scripting.Dictionary")

列出内容:

List(0) = 0-0-0-0
List(1) = 0-1-0-0
List(2) = 0-2-0-0
到目前为止

代码:

    for count = 0 To UBound(List) -1 step 1
        //not sure how to return
    next

我目前所做的不起作用。

所需的退货清单:

 0-0-0-0
 0-1-0-0

2 个答案:

答案 0 :(得分:0)

UBound()用于数组,而不是字典。您需要使用Dictionary对象的Count属性。

' Show all dictionary items...
For i = 0 To List.Count - 1
    MsgBox List(i)
Next

' Show the first two dictionary items...
For i = 0 To 1
    MsgBox List(i)
Next

答案 1 :(得分:0)

您需要使用Dictionary的Items方法。有关详细信息,请参阅here

例如:

Dim a, i

a = List.Items

For i = 0 To List.Count - 1
    MsgBox(a(i))
Next i

或者如果你只想要前2:

For i = 0 To 1
    MsgBox(a(i))
Next i