How to check whether a variant array is unallocated?

后端 未结 5 1049
不知归路
不知归路 2020-12-15 08:16
   Dim Result() As Variant

In my watch window, this appears as

Expression | Value | Type
Result     |       | Variant/Variant()
         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 08:46

    You can use the following in the immediate window:

    ?Result Is Nothing
    ?IsNull( Result )
    ?IsEmpty( Result )
    ?IsMissing( Result )
    

    The first is simply for completeness. Since Result is not an object, Result Is Nothing will throw an error. Empty is for variants that have not been initialized including arrays which have not been dimensioned..

    (Update) In doing some additional checking, I have discovered that IsEmpty will never return true on a declared array (whether Redim'd or not) with only one exception. The only exception I found is when the array is declared at the module level and not as Public and then only when you check it in the immediate window.

    Missing if for optional values passed to a function or sub. While you cannot declare Optional Foo() As Variant, you could have something like ParamArray Foo() As Variant in which case if nothing is passed, IsMissing would return true.

    Thus, the only way to determine if the array is initialized is to write a procedure that would check:

    Public Function IsDimensioned(vValue As Variant) As Boolean
        On Error Resume Next
        If Not IsArray(vValue) Then Exit Function
        Dim i As Integer
        i = UBound(Bar)
        IsDimensioned = Err.Number = 0
    End Function
    

    Btw, it should be noted that this routine (or the library posted by Jean-François Corbett) will return false if the array is dimensioned and then erased.

提交回复
热议问题