在经典ASP中,如果动态数组中包含元素,如何获取?

时间:2011-11-29 10:04:48

标签: arrays asp-classic vbscript dynamic-arrays

如果我声明一个像这样的动态大小的数组

Dim myArray()

那么如果这个数组为空或者包含元素,我怎么能进入代码呢?

我尝试使用IsArray(myArray)函数,让我永远为真,

否则如果我尝试UBound(myArray)函数,我会收到错误。

有什么想法吗?提前谢谢,

最高

5 个答案:

答案 0 :(得分:2)

声明数组后,必须对其进行初始化:

Dim myArray()
ReDim myArray(-1)

然后这样的代码将始终有效:

If UBound(myArray)<0 Then
    'array is empty....
Else  
    'array not empty....
End If

编辑:因为你无法初始化数组,所以这里有更长的方法来检查它是否为空:

Dim x, myCount
myCount = 0
If IsArray(myArray) Then
    For Each x In myArray
        myCount = myCount + 1
    Next
End If
If myCount=0 Then
    'array is empty....
Else  
    'array not empty....
End If

答案 1 :(得分:1)

我找到了一个解决方案,我写了一个特定的函数来检查数组是否为null;该函数不检查它是否包含元素,但仅当数组声明为动态而没有维度且没有元素时。

Dim dynamic_array()                         'array without a dimension
Dim empty_array(0)                          'array with a dimension but without an element inside
Dim full_array(0) : full_array(0) = "max"   'array with a dimension and with an element inside


Function IsNullArray(input_array)
    On Error Resume Next
    Dim is_null : is_null = UBound(input_array)
    If Err.Number = 0 Then
        is_null = False
    Else
        is_null = True
    End If
    IsNullArray = is_null
End Function


If IsNullArray(dynamic_array) Then

    Response.Write("<p>dynamic array not 'ReDimed'</p>")

End If

If Not IsNullArray(empty_array) Then

    Response.Write("<p>" & UBound(empty_array) & "</p>") 'return the last index  of the array

End If

If Not IsNullArray(full_array) Then

    Response.Write("<p>" & full_array(UBound(full_array)) & "</p>") 'return the value of the last element of the array

End If

答案 2 :(得分:1)

首先注意一些。

  1. 在VBScript中使用Dim A()并不实用,最好使用ReDim A(n)
  2. 例如ReDim A(-1)也是空数组(没有元素),但已初始化
  3. 作为谈话程序的最佳方式是通过例子......

    Dim a(), b(0), c
    c = Array(a, b)
    ReDim d(-1)
    
    WScript.Echo "Testing HasBound:"
    WScript.Echo "a = " & HasBound(a) & ",", _
                 "b = " & HasBound(b) & ",", _
                 "c = " & HasBound(c) & ",", _
                 "d = " & HasBound(d)
    WScript.Echo "Testing HasItems:"
    WScript.Echo "a = " & HasItems(a) & ",", _
                 "b = " & HasItems(b) & ",", _
                 "c = " & HasItems(c) & ",", _
                 "d = " & HasItems(d)
    
    '> Testing HasBound:
    '> a = False, b = True, c = True, d = True
    '> Testing HasItems:
    '> a = False, b = True, c = True, d = False
    
    Function HasBound(anyArray)
        On Error Resume Next
        HasBound = UBound(anyArray)
        HasBound = (0 = Err)
        On Error Goto 0
    End Function
    
    Function HasItems(anyArray)
        For Each HasItems In anyArray
            HasItems = 1
            Exit For
        Next
        HasItems = (HasItems > 0)
    End Function
    

    如您所见,2个具有不同目的的功能。差异在数组d上可见,其中&#34;具有边界&#34;但是&#34; has-not-items&#34;。

答案 3 :(得分:0)

我现在能想到的一件事是:

On Error resume next
if UBound(myArray) < 0 then response.write "Empty array" end if

编辑:Max的评论

答案 4 :(得分:0)

我总是检查UBound = 0,第一个元素也是空的:

If UBound(myArray) = 0 Then
    if myArray(0) = "" then ''Depending on the type of the array
       ''array is empty....
    End If
End If