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
Yes, you can create arrays of any type with length zero.
#3) Way 3 : array1 = Array(1,2,3,4,5,6) Here, Array Function is used to declare an array with a list of arguments inside the parenthesis and all integer values are passed directly inside the parenthesis without any need of mentioning the size of an array. Note: The index value of an Array can never be a negative value.
Redim Statement Preserve − An Optional parameter used to preserve the data in an existing array when you change the size of the last dimension. varname − A Required parameter, which denotes Name of the variable, which should follow the standard variable naming conventions.
If you want to resize an array, you use the ReDim <keyword>. Please be advised that you can resize an array only if it has been created without declaring any elements in the array. If you find that you need to continually re-dimension a given array, you use the Preserve <keyword> to keep the data in the array intact.
For:
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)
Will return -1 for arr1, but "VBScript runtime error: Subscript out of range: 'UBound'" for arr2 and arr3.
A general purpose function to test if an array is "Dimmed" or "Empty" should also (probably) test if the variable is actually an array.
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)
For me, 99% of the time when I am checking if an array is "Dimensioned", is if I need to get the UBound of the array and I want to prevent a runtime-error in the cases where the array is not dimensioned. So I will usually pass the UBound as a parameter like:
Function IsDimmedArray(arrParam, intUBoundParam)
intUBoundParam = 0
...
I don't know if this practice actually saves any "Time", but it does save 1 line of code with nearly every use, and is an easy way to enforce the practice of error checking.
Also, I include it for completeness, but in practice, the checking of "UBound >= 0" in IsDimmedArray:
If (llngError = 0) And (lintUBound >= 0) Then : IsDimmedArray = True
is typically not necessary because usually it will be used in cases like:
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
So, in this case, lintUBound = -1 and the For ... Next will be skipped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With