如何在VBScript中创建集合对象?

11

以下代码使用create object时应当传入哪些参数?

dim a
set a=CreateObject("Collection") //getting a runtime error saying ActiveX 
//component can't create object: 'Collection
a.add(CreateObject("Collection"))
a.Items(0).Add(1)
MsgBox(a.Items(0).count)
MsgBox(a.Items(0).Item(0))

通常你会这样导入:CreateObject("library.class")。我猜你可能漏掉了其中的一部分。 - bdhar
CreateObject("library.class") - 在这里我们应该提供哪个库和类? - 我们正在寻找这些细节.. - Onnesh
3个回答

17

如何考虑使用字典

Set coll = CreateObject("Scripting.Dictionary")
coll.Add 0, "5"
coll.Add 4, "10"
coll.Add "textkey", "15"
MsgBox coll.Count
MsgBox coll.Item(0)
MsgBox coll.Item(4)
wholeColl = ""
for each key in coll.Keys
  wholeColl = wholeColl & key & " = " & coll.Item(key) & ", "
next
MsgBox wholeColl

8

这里是代码,它很强大:

Option Explicit

dim list
Set list = CreateObject("System.Collections.ArrayList")
list.Add "Banana"
list.Add "Apple"
list.Add "Pear"

list.Sort
list.Reverse

wscript.echo list.Count                 ' --> 3
wscript.echo list.Item(0)               ' --> Pear
wscript.echo list.IndexOf("Apple", 0)   ' --> 2
wscript.echo join(list.ToArray(), ", ") ' --> Pear, Banana, Apple

这促使我安装了.NET,但安装后它运行得非常良好。 - Mark E.

0

for:

dim lista : set lista = CreateObject("Scripting.Dictionary")

你可以像这样迭代:

dim key
for each key in lista.keys
    Wscript.Echo key & " = " & lista.item(key)
next

已合并到Tester101的答案中。 - Sandra Rossi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接