in Classic ASP, How to get if a dynamic array has elements inside?

我与影子孤独终老i 提交于 2019-12-06 07:26:31

问题


If I declare a dynamic sized array like this

Dim myArray()

Then how I can get in the code if this array is empty or it contains elements?

I tried with IsArray(myArray) function that give me always True,

otherwise if I try with UBound(myArray) function, I get an error.

Any ideas? thanks in advance,

Max


回答1:


After declaring the array, you have to initialize it:

Dim myArray()
ReDim myArray(-1)

Then such code will always work:

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

Edit: as you can't initialize the array, here is longer way to check if it's empty or not:

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



回答2:


I found a solution, I wrote a specific function to check if an array is null or not; the function doesn't check if it has elements inside but only if the array is declared as dynamic without dimensions and no elements.

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



回答3:


First some notes.

  1. Using Dim A() is not so practical in VBScript, better use ReDim A(n).
  2. For example ReDim A(-1) is also empty array (no elements) but initialized.

And as the best way coders to talk is by examples...

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

As you see, 2 functions with different purpose. The difference is visible on array d which "has-boundary" but "has-not-items".




回答4:


The one thing I can think of right now is:

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

EDIT: Max's comment




回答5:


I've always checked for UBound = 0 and the first element is empty too:

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


来源:https://stackoverflow.com/questions/8308921/in-classic-asp-how-to-get-if-a-dynamic-array-has-elements-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!