Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a dynamic array has not be dimensioned in VBScript

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
like image 840
Eric Anastas Avatar asked Dec 17 '10 01:12

Eric Anastas


People also ask

How can you tell if a dynamic array is empty?

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.

What is ReDim preserve in VBScript?

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.

What is UBound in VBScript?

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.

How is an array declared in VBScript?

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.


1 Answers

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
like image 78
Hans Olsson Avatar answered Nov 01 '22 15:11

Hans Olsson