Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to Detect an Empty Array

Posted on 2003-03-19
6
Medium Priority
?
894 Views
Last Modified: 2010-08-05
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.

GeneM
0
Comment
Question by:GeneM
6 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 200 total points
ID: 8167018
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.
0
 

Expert Comment

by:itzme
ID: 8167020

This should do the trick:

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

      Exit Sub

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

   End Sub


0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8167038
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8167039
On error resume next
a = Myarray(0)
If err then 'Array not initalized
   err.clear
   Iniyt Array
End if



Regards


John McCann
0
 

Expert Comment

by:R2DTOEA
ID: 8167190
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.
0
 
LVL 3

Author Comment

by:GeneM
ID: 8170779
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.

GeneM
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question