繁体   English   中英

VBScript 中的零长度数组

[英]Zero-length arrays in VBScript

我必须做一些 ASP 工作,但我发现该语言没有提供检测零长度数组的方法(好吧,我想您可以检测到它们在尝试使用它们时抛出的异常......)。 如果没有任何理智的方法来处理它,为什么 Split() 会返回一个空数组? 或者我错过了什么?

我编造了以下 hack 来检测空数组,但必须有更简单的方法。 是哪个? TIA

function ArrayEmpty (a)
    dim i, res
    res = true
    for each i in a
        res = false
        exit for
    next
    ArrayEmpty = res
end function

为了:

Dim arr1 : arr1 = Array()
Dim arr2
Dim arr3 : ReDim arr3(1) : Erase arr3
WScript.Echo UBound(arr1)
WScript.Echo UBound(arr2)
WScript.Echo UBound(arr3)

将为 arr1 返回 -1,但为 arr2 和 arr3 返回“VBScript 运行时错误:下标超出范围:'UBound'”。

用于测试数组是否为“变暗”或“空”的通用函数也应该(可能)测试变量是否实际上是一个数组。

Function IsDimmedArray(arrParam)

Dim lintUBound : lintUBound = 0
Dim llngError  : llngError = 0

    IsDimmedArray = False
    If Not IsArray(arrParam) Then : Exit Function

 '' Test the bounds
    On Error Resume Next

        lintUBound = UBound(arrParam)
        llngError = Err.Number
        If (llngError <> 0) Then : Err.Clear

    On Error Goto 0
    If (llngError = 0) And (lintUBound >= 0) Then : IsDimmedArray = True

End Function                  ' IsDimmedArray(arrParam)

对我来说,99% 的时间当我检查数组是否“维度”时,是我是否需要获取数组的 UBound 并且我想在数组未维度的情况下防止运行时错误。 所以我通常会将 UBound 作为参数传递,例如:

Function IsDimmedArray(arrParam, intUBoundParam)
    intUBoundParam = 0
    ...

我不知道这种做法是否真的节省了任何“时间”,但它几乎每次使用都节省了 1 行代码,并且是一种强制执行错误检查做法的简单方法。

此外,为了完整性,我将其包括在内,但在实践中,检查 IsDimmedArray 中的“UBound >= 0”

    If (llngError = 0) And (lintUBound >= 0) Then : IsDimmedArray = True

通常没有必要,因为通常它会在以下情况下使用:

Dim arrX
Dim lintUBound
Dim intNdx

arrX = Array()
lintUBound = UBound(arrX)
WScript.Echo "arrX is an array with UBound=" & lintUBound

For intNdx = 0 to lintUBound
    WScript.Echo "This will not print: " & intNdx
Next

因此,在这种情况下, lintUBound = -1并且 For ... Next 将被跳过。

使用Array函数创建或由其他内部 VBScript 函数(例如Split返回的空数组的上限为 -1。 所以你可以像这样测试一个空数组:

Dim arr : arr = Array()

If UBound(arr) >= 0 Then
  ' arr is non-empty
Else
  ' arr is empty
End If

更多信息:测试空数组

如果您的方法应该能够返回一个空数组,您的代码必须是这样的

Option Explicit

dim result : result = mymethod
if(NOT ubound(result) > 0) then MsgBox "Array Is Empty"
dim elem : for each elem in result
  MsgBox "Element"
Next

Function mymethod
  dim results : results = Array()
  mymethod = results
End Function

Array() 创建一个 Ubound = -1 数组,在 for each 循环中没有循环。

我认为这是在 VBS 中检查数组的好方法

Dim myArray
myArray = Array()
sTest = IsArrayEmpty(myArray)
Msgbox (sTest) ' True
Function IsArrayEmpty(myArray)
    iRet = True

    If IsArray(myArray) Then
        i = 0
        For Each e In myArray
            If Not IsEmpty(e) And Len(e)>0 Then
                i = i +1
            End If
        Next
        If i>0 Then
            iRet = False
        End If
    End If
    wIsArrayEmpty = iRet
End Function

我在TechNet上发现了一个建议使用VarType检查数组的线程:

Function ArrayEmpty (a)
    If VarType(a) < 8192 Then ArrayEmpty=True Else ArrayEmpty=False
End Function

它对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM