How to Detect an Empty Array

Hi all,

This should be easy, but I cannot seem to find the answer.

I have an array which I initialize empty:

Dim MyArray() as MyUserDataType

Now in my code, I want to determine the UBOUND of MyArray.  If I do something like this:

for i = 0 to ubound(Myarray) then.....

I get an error because MyArray is empty at this point.  How can I test if MyArray is empty and I should not be testing for the ubound?  By the way, I need to use base 0 if that makes any difference.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sorry, but there is no simple way other than error checking to find if your array is dimensioned.  You can do this:

    Dim x() As MyDataType
    Dim i As Long
    On Error Resume Next
    i = UBound(x)
    If Err.Number = 9 Then
        MsgBox "Empty Array"
    End If
    On Error GoTo 0

But it is best to dimension an array as soon as you can to avoid this error.  If you need to create an uninitialized array that you can test, do this:

Dim x() as MyDataType
Redim x(-1 to -1)

Now when you check the Ubound, it will return -1 indicating an uninitialized array.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

This should do the trick:

   On Local Error GoTo empty_array
      Dim iLocation As Integer
      iLocation = UBound(myArray)

      Exit Sub

      If Err.Number = 9 Then
        'theres no data in array
      End If

   End Sub

By the way, for simple arrays you can wrap the error testing in a function, but it is problematic with usertype arrays because they are not passed as parameters so easily.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

On error resume next
a = Myarray(0)
If err then 'Array not initalized
   Iniyt Array
End if


John McCann
The easiest thing to do, though it takes a little more memory is to alter how you define the array.

Instead of:
Dim MyArray() as MyUserDataType
Change it to:
Dim MyArray

This will make it a variant.  Then before checking the ubound do a:
if isarray(myarray) then
   'do the ubound checks.
end if

If it is still a variant, then it hasn't been set yet.  Of course, when you want to set it, you have to redim before using it, unless you are splitting data into it.

There is also a isempty function, but I don't know if you can use that for a defined array.
GeneMAuthor Commented:
Thanks to all who answered.

I accepted Paul's solution because he was the first to talk about intercepting the error.  I also liked his solution of doing the Redim x(-1 to -1).  I didn't know it would accept negative bounds!  

The use of the variant was a good solution.  However, I personally just don't like building arrays in a variant.  I cannot defend that position.  Its just not my style! :-)

It just seems that VB should offer a way to test for an "empty" array.

Again, Thanks to all.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.