开发者

How to create collection object in vbscript?

what should be the parameter for create object the following code

dim a
set a=CreateObject("Collection") //getting a runtime error saying ActiveX 
//component ca开发者_Go百科n't create object: 'Collection
a.add(CreateObject("Collection"))
a.Items(0).Add(1)
MsgBox(a.Items(0).count)
MsgBox(a.Items(0).Item(0))


how about a Dictionary

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


Here is the code, its powerful:

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


for:

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

you can iterate like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜