Как создать объект коллекции в VBScript? - PullRequest
8 голосов
/ 19 мая 2010

каким должен быть параметр для 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))

Ответы [ 3 ]

14 голосов
/ 19 мая 2010

как насчет словаря

Set a = CreateObject("Scripting.Dictionary")
a.Add 0, "5"
a.Add 4, "10"
MsgBox a.Count
MsgBox a.Item(0)
MsgBox a.Item(4)
3 голосов
/ 19 мая 2017

Вот код, его мощный:

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 голосов
/ 29 октября 2014

для:

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

Вы можете повторить, как это:

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