vbscript - Come posso attraversare una raccolta in ASP classico?

original title: "vbscript - How do I traverse a collection in classic ASP?"


I want to be able to do:

For Each thing In things
End For


Voglio essere in grado di fare: per ogni cosa nelle cose End for CLASSIC ASP - NOT .NET!

Questo è il riepilogo dopo la traduzione, se è necessario visualizzare la traduzione completa, fare clic sull'icona "traduci"

Tutte le risposte
  • Translate

    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.

  • Translate

    Something like this?

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

    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
        ' Do something else 
    end if

  • Translate

    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 />"

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


    You can also do this

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

    Which would produce

     Name: Scott
     Age: 20

  • Translate

    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

  • Translate

    As Brett said, its better to use a vb component to create collections. Dictionary objects are not very commonly used in ASP unless for specific need based applications.

  • Translate

    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 needed, use instead If objDic.Exists("varName").

    Just spend a few days knocking walls, with this Mcrosoft "feature"...