如何遍历经典ASP中的集合?

我希望能够做到:

For Each thing In things
End For

CLASSIC ASP - 不.NET!

0
额外 编辑
意见: 3

6 答案

无论你的[东西]需要写在VBScript之外。

在VB6中,你可以编写一个自定义集合类,然后你需要编译到一个ActiveX DLL并将其注册到您的网络服务器上以访问它。

0
额外

像这样?

dim cars(2),x
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"

For Each x in cars
  response.write(x & "
") Next

请参阅 www.w3schools.com

如果您想将关键字和值关联起来,请使用字典对象

Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"
if objDictionary.Exists("Name") then
    ' Do something
else
    ' Do something else 
end if
0
额外

我之前使用过的一种方法是使用返回数组的属性,该数组可以迭代。

Class MyCollection
    Public Property Get Items
        Items = ReturnItemsAsAnArray()
    End Property
    ...
End Class

迭代如下:

Set things = New MyCollection
For Each thing in things.Items
    ...
Next
0
额外

Be VERY carefully on using VB Script Dictionary Object!
Just discover this "autovivication" thing, native on this object: http://en.wikipedia.org/wiki/Autovivification

So, when you need to compare values, NEVER use a boolen comparison like:
If objDic.Item("varName") <> "" Then...
This will automatically add the key "varName" to the dictionary (if it doesn't exist, with an empty value) , in order to carry on evaluating the boolean expression.

如果需要,请改用 If objDic.Exists(“varName”)

Just spend a few days knocking walls, with this Mcrosoft "feature"...
vbscript-dictionary-object-creating-a-key-which-never-existed-but-present-in-another-object

0
额外

你将要得到的最接近的是使用一个字典(如Pacifika所提到的)

Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.CompareMode = vbTextCompare 'makes the keys case insensitive'
objDictionary.Add "Name", "Scott"
objDictionary.Add "Age", "20"

但是我通过我的字典像集合一样循环

For Each Entry In objDictionary
  Response.write objDictionary(Entry) & "
" Next

你可以通过这种方式遍历整个字典,写出看起来像这样的值:

Scott
20

你也可以做到这一点

For Each Entry In objDictionary
  Response.write Entry & ": " & objDictionary(Entry) & "
" Next

哪会产生

 Name: Scott
 Age: 20
0
额外

正如Brett所说,使用vb组件创建集合更好。字典对象在ASP中不常用,除非是针对特定需求的应用程序。

0
额外