ASP 字典 [Key, Array] 对

ASP Dictionary [Key, Array] pair

目标

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

问题:

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

我有什么

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

我将值设置为:

myDict.add someKey, someArray

我检索:

myDict.Item(someKey)(somePosition)

但是,这不起作用:

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

我认为那行不通。要修改您需要删除项目并添加新项目的值。

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

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"

当我只需要一个可变的项目列表时,我会像这样使用字典:

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