English 中文(简体)
如何将字典添加到数组
原标题:How to add dictionary to array
  • 时间:2012-05-23 10:38:23
  •  标签:
  • vbscript

简单示例 :

Dim d, a(0)
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
Set a(0) = d

WScript.Echo(TypeName(a))

产出:

variant () ()

我看不出有什么方法可以进入这个物体,它计为3, 但它看起来是空的

我感到惊讶的是,谷歌没有列出任何有用的结果来解决这个问题—— 如何用数组来分配字典?

我发现最接近的就是这个未解 < a href=>""http://www.tek-tips.com/viewthread.cfm?qid=1397563" rel="no follow" >link

我的问题是,我有一个循环 创建临时字典, 我计划用一个简单的维数组 输入字典对象, 但没有成功

我还发现了类似这个link ,其中为字典键和词典项目(价值)分别创建了两个阵列,这对我的案件来说太复杂了。

VBScript完全有可能吗?

最佳回答

我可能误解了你的问题 但你可以这样读字典

WScript.Echo(a(0).Item("b"))

Edidi

此代码 :

Dim d, a(0)
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
Set a(0) = d
WScript.Echo(a(0).Item("b"))

dim x
x = d.Items

dim i
for i = lbound(x) to ubound(x)
    WScript.Echo(x(i))
next

在我的Windows 7 机器上生成此输出 :

C:>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Belgrade
Athens
Belgrade
Cairo

C:>
问题回答

例如:

Dim d
Set d = CreateObject("Scripting.Dictionary") 
d.Add "a", "Athens" 
d.Add "b", "Belgrade" 
d.Add "c", "Cairo" 

WScript.Echo(d("a"))  => Athens

d("a") = "Brussels"

WScript.Echo(d("a"))  => Brussels

a = d.Keys     Get the keys.
b = d.Items
For i = 0 To d.Count -1   Iterate the array.
  wscript.echo a(i) & " " & b(i)
Next


 =>a Brussels
 =>b Belgrade
 =>c Cairo




相关问题
What approach should I use to test a VBScript?

I ve been asked to help out with a project which has made extensive use of VBScript to process a whole bunch of text files and generate certain outputs - sanitized files, SQL entries etc.. The script ...

Unable to call c# code from vbscript - ActiveX error

I am trying to call a method I have written in C# from VBScript. I have followed just about all of the instructions I can find on the web and am still having problems. Specifically I am getting Error:...

How to set delay in vbscript

How to set delay in vbscript? WScript.Sleep(100) does not work on Windows XP, Vista.

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

How to check which Operating System?

How can I check OS version in a batch file or through a vbs in an Windows 2k/2k3 environment ? You know ... Something like ... : "If winver Win2k then ... or if winver Win2k3 then ....

Problem casting field when querying spreadsheet

I am trying to query an .xls spreadsheet with VBScript, but I have run into an issue when trying to cast a field. I connect to the spreadsheet like this. Set objConnection = CreateObject("ADODB....

热门标签