开发者

Classic ASP Ubound returns 9 getting subscript out of range

Sorry if this has been covered, I couldn't find anything specifically to this issue in my searches.

I am trying to debug a classic ASP application. I need to print the session variables, one of which is an array. My code is below, I keep getting Subscript out of range, usually this means that the array is empty (Ubound returns -1) but in this case it's coming back as 9. I've tried For i = 1 To 4 and For i = 0 To 4 with the same results.

 For Each Item In Session.Contents 
     If IsArray(Session(item)) Then 

        localArray = Session(item) 
        Response.Write "<h1>Ubound = " & Ubound(localArray) & "</h1> <br />" //getting Ubound = 9 here

        For i = 1 To Ubound(localArray)
           Response.Write "<br>&nbsp;&nbsp;" & item 
           Response.Write "(" & i & ") = " & localArray(i) 
        Next 

    Elseif IsObject(Session(item)) Then 
        Response.Write "<b>" & item & " is an object </b>" 
    Else 
        Response.Write item & " = " & Session(item) 
    End If 
    Response.Write "<br>" 
Next 

EDIT

Changed code to

For i = LBound(localArray) To UBound(localArray)

Have also tried

localArray = Session(item)
Response.Write localArray(2) //since UBound returns 9 figured 2nd index should be safe

I still receive the error, it seems like the array may 开发者_开发知识库not be single dimension. However I am not familiar with the structure or creation of this session variable, is there a way to get the structure of an array in ASP?


I was able to use the answer here: How many dimensions in my array or get the last one to obtain the size of the array. I changed my code to:

            localArray = Session(item) 

        colStart = LBound(localArray, 1)
        colEnd = UBound(localArray, 1)
        rowStart = LBound(localArray, 2)
        rowEnd = UBound(localArray, 2)

        For row = rowStart To RowEnd
            For col = colStart To colEnd
                Response.Write localArray(col,row) & "<br />"
            Next
        Next

So today I learned Subscript out of range on an array you know is not out of range, means that it is not a single dimensional array.


Assuming that localArray is a single dimensional array, change the code to

For i = LBound(localArray) To Ubound(localArray)
   Response.Write "<br>&nbsp;&nbsp;" & item 
   Response.Write "(" & i & ") = " & localArray(i) 
Next 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜