ASP字典[Key,Array]对

时间:2015-09-28 13:42:00

标签: arrays dictionary vbscript asp-classic

目标

我正在尝试创建一个在ASP中存储键(字符串)和值(数组)对的字典。

问题:

似乎ASP能够将数组存储为值,并且能够返回值(在数组中的任何位置),但是它无法更新值?

我有什么

Dim myDict
Set myDict=Server.CreateObject("Scripting.Dictionary")

我用以下值设置值:

myDict.add someKey, someArray

我检索:

myDict.Item(someKey)(somePosition)

然而,这不起作用:

myDict.Item(someKey)(somePosition) = "Hello"

2 个答案:

答案 0 :(得分:1)

我不认为这会起作用。要修改删除项目所需的值并添加新项目。

答案 1 :(得分:1)

为什么不定义一个类而不是一个数组?是的,ASP已经在某种程度上支持了类。

Class MyType
   public Value1
   public Value2
   public Value3
End Class

将它们插入字典中,如下所示:

dim myVal

Set myVal = new MyType
myVal.Value1 = 1
myVal.Value2 = "foo"
myVal.Value3 = "bar"

Set myDict.Item("key") = myVal

并像这样修改:

myDict.Item("key").Value2 = "baz"

编辑:在每个项目中包含可变数量的值:

Set myDict.Item("key") = Server.CreateObject("Scripting.Dictionary")

myDict.Item("key").Item("var name") = "baz"

当我只需要一个可变的Items列表时,我会像这样使用Dictionary:

dim myDict
Set myDict = Server.CreateObject("Scripting.Dictionary")

myDict.Item(myDict.Count) = "foo"
myDict.Item(myDict.Count) = "bar"
myDict.Item(myDict.Count) = "baz"

...并在一个简单的循环中获取值:

for each myValue in myDict.Items
    Response.Write "Value is: " & myValue & "<br />"
next