【问题标题】:Classic ASP Ubound returns 9 getting subscript out of range经典 ASP Ubound 返回 9 使下标超出范围
【发布时间】:2010-10-25 18:40:47
【问题描述】:

很抱歉,如果这已涵盖,我在搜索中找不到任何专门针对此问题的内容。

我正在尝试调试经典的 ASP 应用程序。我需要打印会话变量,其中一个是数组。我的代码在下面,我不断让下标超出范围,通常这意味着数组是空的(Ubound 返回 -1)但在这种情况下它会返回为 9。我尝试过 For i = 1 To 4For i = 0 To 4结果相同。

 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 

编辑

代码改为

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

也试过

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

我仍然收到错误,看起来数组可能不是一维的。但是我不熟悉这个会话变量的结构或创建,有没有办法在 ASP 中获取数组的结构?

【问题讨论】:

    标签: arrays asp-classic session-variables


    【解决方案1】:

    我可以在这里使用答案:How many dimensions in my array or get the last one 来获取数组的大小。我将代码更改为:

                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
    

    所以今天我在一个数组上了解到Subscript out of range,你知道它没有超出范围,这意味着它不是一维数组。

    【讨论】:

      【解决方案2】:

      假设localArray是一维数组,把代码改成

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

      【讨论】:

      • 也尝试过这种方法,但没有成功。我不确定数组是如何构建的,因为我正在调试我没有编写的代码......我在想我的问题可能是它不是一维数组。有什么方法可以知道数组的结构是什么?
      猜你喜欢
      • 2020-03-05
      • 1970-01-01
      • 2019-02-06
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 2022-08-21
      相关资源
      最近更新 更多