Say I create a dynamic array in VBScript
Dim myArray()
Later on how can I check to see that this array has never been dimensioned?
Ubound(myArray) 'sub script out of range
Lbound(myArray) 'sub script out of range
IsEmpty(myArray) 'returns false
Therefore, we came up with this solution: all we do is try and retrieve the upper bound (i.e., the index number of the last item) in the array. If the call to the Ubound function succeeds, then the array must have at least one item in it. If the call fails, then the array must be empty.
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.
The UBound function returns the largest subscript for the indicated dimension of an array. Tip: Use the UBound function with the LBound function to determine the size of an array.
Declaration of Arrays in VBScript Declaration of an Array can be done in the same manner in which Variables are declared but with the difference that the array variable is declared by using parenthesis '()'. The Dim keyword is used to declare an Array.
I don't think there's anything built in, but you can create your own function as:
Function IsInitialized(a)
Err.Clear
On Error Resume Next
UBound(a)
If (Err.Number = 0) Then
IsInitialized = True
End If
End Function
Which you can then call as:
Dim myArray()
If Not IsInitialized(myarray) Then
WScript.Echo "Uninitialized"
End If
However, one way to work around it might be to not declare empty arrays, instead declare just a variable and set it to an array later, so change the code above to:
Dim myArray
myArray = Array()
If Not IsInitialized(myarray) Then
WScript.Echo "Uninitialized"
End If
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