Search code examples
asp-classicvbscript

Classic ASP - Dynamic array not working


Im trying to create an array from a string i have but its not working, i was wondering if someone could let me know where im going wrong.

There could be quite a few filenames in the string, which is dynamic

My string is as follows:

imagesArray = "1.jpg,2.jpg,3.jpg,4.jpg,5.jpg"

My code is as follows:

fileNameArray = split(imagesArray, ",")

Dim newImageArray
Redim newImageArray(uBound(fileNameArray) + 1)

For each i in fileNameArray
    newImageArray(i) = i
Next

I keep getting a 500 error when i run this.


Solution

  • The reason it's not working is you're using a For Each loop, where i is the object. You're then using the object as the index on the array as well as the value.

    If you use the following, the code will work -

    For i = 0 to UBound(fileNameArray)
        newImageArray(i) = i
    Next
    

    I suspect what you're actually looking for is -

    fileNameArray = split(imagesArray, ",")
    
    Dim newImageArray
    Redim newImageArray(uBound(fileNameArray))
    
    For i = 0 to uBound(fileNameArray)
        newImageArray(i) = fileNameArray(i)
    Next