在经典 ASP 中,如何获取动态数组内部是否有元素?

发布于 2024-12-18 10:49:26 字数 249 浏览 4 评论 0原文

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

Dim myArray()

,那么如果该数组为空或包含元素,我如何进入代码?

我尝试使用 IsArray(myArray) 函数,该函数始终为 True,

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

有什么想法吗?预先感谢,

马克斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

束缚m 2024-12-25 10:49:26

声明数组后,您必须初始化它:

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

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
财迷小姐 2024-12-25 10:49:26

首先是一些注释。

  1. 在 VBScript 中使用 Dim A() 不太实用,最好使用 ReDim
    A(n)。
  2. 例如,ReDim A(-1) 也是空数组(无元素),但已初始化

程序员最好的交流方式是通过示例......

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

如您所见,两个函数具有不同的目的。差异在数组 d 上可见,它“有边界”但“有非项目”。

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".

笔落惊风雨 2024-12-25 10:49:26

我找到了一个解决方案,我编写了一个特定的函数来检查数组是否为空;该函数不会检查它内部是否有元素,但仅检查该数组是否被声明为动态的,没有维度且没有元素。

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

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
如果没有 2024-12-25 10:49:26

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

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

编辑:麦克斯的评论

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

胡渣熟男 2024-12-25 10:49:26

我总是检查 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文