tags:

views:

92

answers:

1

I have to do some ASP work and I found out that the language doesn't provide a way to detect zero-length arrays (well, I suppose you can detect the exception they throw when you try to use them...). Why would Split() return an empty array if there isn't any sane way to handle it? Or am I missing something?

I concocted the following hack to detect empty arrays, but there has to be an easier way. Which is it? TIA

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

An empty array created using the Array function or returned by other intrinsic VBScript functions, such as Split, has an upper bound of -1. So you can test for an empty array like this:

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

More info here: Testing for Empty Arrays.

Helen
Your code is incorrect because UBound fails on empty arrays. The page you linked confirms my suspicion that the only way is by detecting that an exception is thrown (or by my "for each" hack). That's sad :-/
angus