Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if an array is empty?

Status
Not open for further replies.

davidd31415

Programmer
Jan 25, 2006
154
US
How can I check if an array is empty?

I've read references to IsEmpty being used with arrays but it always returns false on me. The only solution I've found is to use "if (not array)" but I don't understand how it works exactly... For example:

When I run the program "test" below:

1. The variable tester is true both times it is defined.

2. After s is re-dimensioned, the last two if statements evaluate true (a message box is displayed with the text "it is not empty" followed by one with "it is empty."

From what I see here it looks like 'if (not s)' is only valid when the array is empty- is there a function that returns a true or false value instead of only a true or not true value?

Sub test()
Dim s() As Integer
Dim tester As Boolean

tester = Not s
If (Not s) = True Then MsgBox ("it is empty")
If (Not s) = False Then MsgBox ("it is not empty")

If (Not s) <> True Then MsgBox ("it is not empty")
If (Not s) <> False Then MsgBox ("it is empty")

ReDim Preserve s(0) As Integer

tester = Not (s)
If (Not s) = True Then MsgBox ("it is empty")
If (Not s) = False Then MsgBox ("it is not empty")

If (Not s) <> True Then MsgBox ("it is not empty")
If (Not s) <> False Then MsgBox ("it is empty")
End Sub
 


Hi,
Code:
    Dim a(), i
    
    On Error Resume Next
    
    i = UBound(a)
    If Err.Number <> 0 Then
        MsgBox "error"
    Else
        MsgBox "NO error"
    End If

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Have this function from Francesco Balena

Function NumberOfDims(arr As Variant) As Integer
Dim dummy As Long
On Error Resume Next
Do
dummy = UBound(arr, NumberOfDims + 1)
If Err Then Exit Do
NumberOfDims = NumberOfDims + 1
Loop
End Function
 

You have your answer in the question.

[blue](Not s) = True[/blue] gives you True or False

Code:
Sub test()
    Dim s() As Integer
    Dim tester As Boolean
    
    tester = (Not s) = True
    
    If tester Then
        MsgBox "Original array is not dimensioned"
    Else
        MsgBox "Original array has dimensions"
    End If
    
    ReDim Preserve s(0) As Integer
    
    tester = (Not s) = True
    
    If tester Then
        MsgBox "ReDimmed array is not dimensioned"
    Else
        MsgBox "ReDimmed array has dimensions"
    End If

End Sub

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Thanks for the replys all.

tester = (Not s) = True seems to be the simplest but does not seem intuitive to me; can you explain why this works (or why some of the things I was initially trying do not work) ? Even better would be a pointer towards a tutorial/book that would throughly cover that.

 
My apologies for not replying sooner. I have been trying to find out (am still trying to find out) quite why this works.

A reference to an array variable in a situation where an array is not appropriate gives, probably reasonably, a 'Type Mismatch' error. A reference where it is appropriate, of course, works just fine and, behind the scenes I guess works with the address of the array.

A reference to "Not an array variable" seems to do a logical Not on the address and returns a number - and, going one stage further, "Not Not an array variable" returns the actual address of the array variable (the equivalent of StrPtr and ObjPtr for (variable length) string and object variables respectively); this rather bizarre construct actually fills a hole in the language.

When an array variable does not have dimensions (dimmed as () and not redimmed) its address is zero; when it has been redimmed and has dimensions it has a non-zero address. A logical Not operation thus returns "Not 0" (= -1) for a dimensionless array and "Not some address" (= something other than -1) for a dimensioned one.

When coerced to Boolean, -1 = True (and 0 = False), but a check of any number against True (or False) is valid and returns False in all but the above cases - that is "-1 = True" returns True and "0 = False" returns True and "any other number = True" and "any other number = False" both return False. There are a few different ways of constructing code round this, of which the "(Not s) = True" construct is one.

As far as I know this is completely undocumented and, thus, unsupported; I hope you can make sense of my explanation - if not, please do come back.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top