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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Length of an empty array

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

How do I check if an array has any elements?

UBound is throwing a 'Subscript out of range error'.

I can't seem to find a method or command to check the length of an array?

Thanks,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
well I've come up with this function...

Code:
Function IsArrayEmpty(vArray As Variant) As Boolean

   Dim lUBound As Long
   
   On Error Resume Next
     
   lUBound = UBound(vArray)
   
   If err.Number <> 0 Then
      IsArrayEmpty = True
   Else
      IsArrayEmpty = False
   End If
   
End Function

Is this the only thing I can do?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
I think that is the common solution. There is not another easy way to do it, that I know.
 
Try thread222-1489658 for some similar solutions and some links to yet more ...


Out of the solutions that work by throwing an exception (such as yours) I rather like Franceso Balena's version. Of the API versions I clearly like my solutions :)

But my absolute favourite is

If(Not vArray) = -1 Then MsgBox "Empty array"
 
1DMF . . .

Also be aware of [blue]default values[/blue] of your variables & functions while programming. They can help keep you from writing unnecessary lines of code.

Taking into account that [blue]IsArrayEmpty[/blue] is set to return a boolean value. We note that its default value is [blue]False[/blue] (aka 0). Keeping this in mind:

Code:
[blue]   If err.Number <> 0 Then
      IsArrayEmpty = True
   Else
      IsArrayEmpty = False
   End If

[purple]changes to:[/purple]
   
   If err.Number <> 0 Then IsArrayEmpty = True[/blue]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
very true!

I lifted the code while hunting the web without actually bothering to condense it , it was more a 'is there a simpler way' , rather than 'is my code condensed'

Having said that all the OU courses I have done would rather you fully code such things than leave code that isn't so easily read.

I'm not sure I like going heavy duty with API!

If I refactor the one liner I guess this would be the most concise?

Code:
Function IsArrayEmpty(vArray As Variant) As Boolean
   
   IsArrayEmpty = (vArray = -1)
   
End Function

But I guess I might as well just code the -1 test without the need for a call to another function, assuming arrays (not variant) vars can be tested for -1?


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
>would rather you fully code such things than leave code that isn't so easily read
Very true

>this would be the most concise?

Not quite
Code:
[blue[Public Function IsArrayEmpty(vArray()) As Boolean
    IsArrayEmpty = (Not vArray) = -1
End Function[/blue]

> assuming arrays (not variant) vars can be tested for -1?

That's not quite what is happening, but yes, you can directly test the array
 
That's not quite what is happening
what is then?

(Not vArray) What is this testing to result in -1 (I assume -1 = True , like checkboxes) ?

is the value of (vArray) its index size or false?


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Check out my 1 Mar 09 08:30 post in thread222-1495917.

If I were to sum up I'd say that we were conning VB ...
 
I'm note sure I would have worked out that it's because it is apply a NOT to a pointer inverting the value of the bytes which gives you -1 if it has no value.

I'm so glad TT has you around!

Any reason MS hasn't given us an IsEmpty, Count, Length or any other function / method for easy checking?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
>Any reason MS hasn't given us an IsEmpty, Count, Length or any other function / method for easy checking?

Well ... they have in .net ...
 
Yes, sorry, should have mentioned that this trick unfortunately doesn't work for string arrays as VB handles them slightly differently. I believe that is mentioned in at least one of the threads previously linked to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top