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

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


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 IfEnd 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 myArraymyArray = Array()If Not IsInitialized(myarray) Then    WScript.Echo "Uninitialized"End If


I prefer to Not the Array, and then compare the result to -1. This works, and does so without intentionally causing an error.

Dim myArray()...If (Not myArray) = -1 Then    ReDim myArray(0)Else    ReDim Preserve(0 To UBound(myArray)+1)End If


I've been using something like this:

Dim arrarr = nullsub addElement (byref arr, element)    if isNull (arr) then        redim arr(0)    else        redim preserve arr (uBound(arr) + 1)    end if    arr(uBound(arr)) = elementend sub