Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I traverse a collection in classic ASP?

I want to be able to do:

For Each thing In things
End For

CLASSIC ASP - NOT .NET!

like image 870
Ronnie Avatar asked Aug 05 '08 13:08

Ronnie


4 Answers

Something like this?

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

For Each x in cars
  response.write(x & "<br />")
Next

See www.w3schools.com.

If you want to associate keys and values use a dictionary object instead:

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
like image 52
svandragt Avatar answered Nov 19 '22 21:11

svandragt


Whatever your [things] are need to be written outside of VBScript.

In VB6, you can write a Custom Collection class, then you'll need to compile to an ActiveX DLL and register it on your webserver to access it.

like image 25
Brett Veenstra Avatar answered Nov 19 '22 22:11

Brett Veenstra


The closest you are going to get is using a Dictionary (as mentioned by Pacifika)

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

But I loop through my dictionaries like a collection

For Each Entry In objDictionary
  Response.write objDictionary(Entry) & "<br />"
Next

You can loop through the entire dictionary this way writing out the values which would look like this:

Scott
20

You can also do this

For Each Entry In objDictionary
  Response.write Entry & ": " & objDictionary(Entry) & "<br />"
Next

Which would produce

 Name: Scott
 Age: 20
like image 2
Skyhigh Avatar answered Nov 19 '22 20:11

Skyhigh


One approach I've used before is to use a property of the collection that returns an array, which can be iterated over.

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

Iterate like:

Set things = New MyCollection
For Each thing in things.Items
    ...
Next
like image 1
Mike Henry Avatar answered Nov 19 '22 20:11

Mike Henry