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

问题描述 投票:0回答:3

CreateObject
的参数应该是什么?

看这个例子:

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))
collections vbscript
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

0
投票

对于:

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

你可以这样迭代:

dim key
for each key in lista.keys
    Wscript.Echo key & " = " & lista.item(key)
next
© www.soinside.com 2019 - 2024. All rights reserved.