【问题标题】:Controlling Empty Arrays in Classic ASP在经典 ASP 中控制空数组
【发布时间】:2012-10-10 15:33:28
【问题描述】:

好的,我是 ASP 的新手。

根据数组中传递的内容,我有一个加载不同内容的客户端。

select case lcase(arURL(4))

但有时,arURL(4) 可能为空,在这种情况下,我会收到以下错误:

错误运行函数functionName(),错误为:

下标超出范围

有人知道解决这个问题的方法吗?

谢谢

按要求确定更多代码。这是可怕的代码,我并不是要让任何人头疼,所以请原谅。再次感谢........

function GetContent()
    dim strURL, arURL, strRetval
    select case lcase(request.ServerVariables("URL"))
        case "/content.asp"
            strURL = ""
            arURL = split(request.querystring("url"), "/")
            if request("page") = "" then
                select case lcase(arURL(2))
                    case "searches"
                        select case lcase(arURL(1))
                            case "looking"
                                select case lcase(arURL(3))
                                    case "ohai"
                                        strRetval = "Lorem"
                                    case "blahblah"
                                        strRetval = "Lorem Ipsum"                        
                                    case "edinburgh"
                                        select case lcase(arURL(4))
                                            case "ohai"
                                                strRetval = "Ipsum"
                                            case "ohno"
                                                strRetval = "Lorem"
                                        end select
                                    case "bristol"
                                        select case lcase(arURL(4))
                                            case "some_blahblah"
                                                strRetval = "LOREM"
                                            case "overthere"
                                                strRetval = "LOREM"
                                            case "blahblah"
                                                strRetval = "LOREM"
                                        end select
                                    case "cambridge"
                                        select case lcase(arURL(4))
                                            case "some_rubbish"
                                                strRetval = "Lorem"
                                        end select
                                    case else
                                        strRetval = " "
                                end select
                            case else
                                strRetval = " "
                        end select
                    case else
                        strRetval = " "
                end select 
            end if
    end select 
    strRetval = strRetval & "<style>h2{border: 0px);</style>"
    GetContent = strRetval
end function

【问题讨论】:

  • 首先您需要知道数组的上限,例如 ubound(arURL)。当您尝试访问不在数组中的索引时会显示此错误

标签: asp-classic


【解决方案1】:

您正在使用通过查询字符串传递的值并将其拆分为“/”字符 - 当该值不包含“足够”的斜杠时,您将收到错误并且代码将崩溃。

例如,如果查询字符串参数url 将只有“/something”,那么即使arURL(2) 也会失败,因为数组只有两项。 (第一个是空字符串,第二个是“something”)

为避免所有这些混乱,我建议的最佳方法是编写自定义函数,该函数将数组和索引作为其参数,如果存在则返回给定索引中的项目,否则为空字符串:

Function GetItemSafe(myArray, desiredIndex, defValue)
    If (desiredIndex < LBound(myArray)) Or (desiredIndex > UBound(myArray)) Then
        If IsObject(defValue) Then
            Set GetItemSafe = defValue
        Else  
            GetItemSafe = defValue
        End If
    Else  
        If IsObject(myArray(desiredIndex)) Then
            Set GetItemSafe = myArray(desiredIndex)
        Else  
            GetItemSafe = myArray(desiredIndex)
        End If
    End If
End Function

(最终得到更通用的版本,让调用代码决定在索引超出数组范围的情况下默认值是多少)

有了这个,改变你的代码使用函数而不是直接访问数组。

以这一行为例:

select case lcase(arURL(2))

应该改为:

select case lcase(GetItemSafe(arURL, 2, ""))

相应地更改这些行的其余部分,当给定值无效时,您将不再收到错误。

【讨论】:

    【解决方案2】:

    这个错误在最基本的层面上说的是你试图从一个不存在的数组元素中获取信息,例如 arURL 可能已经为 3 个元素声明,但访问第 4 个元素会生成“下标超出范围”错误。

    如果您键入数组中的最后一个元素,您可能会查看 UBound() 函数,该函数返回数组中的高索引元素,例如:

    select case lcase(arURL(ubound(arURL))
    

    但是,代码中可能会发生其他事情,这会改变您确定应将哪个元素用作“选择案例”的目标的方式,因此建议发布更多代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 2011-11-17
      相关资源
      最近更新 更多