VB6: How do I detect if an array is empty?

I have a type with a bunch of string arrays in it like so (shortened for viewing pleasure):

Private Type TCriteria
    provider() As String
End Type

Then, various strings are passed to me and I use the Split command to parse them out and put the results into these arrays.

So, depending on the strings passed to me, I will load up anywhere from none to all of the arrays.

Is there a way to tell if one of these arrays has NOT been loaded up yet?

I've tried IsEmpty() IsNull() and they only work on Variants.

Any attempt to reference this array gives a "subscript out of range" error.

any hints?

Who is Participating?
jcastrConnect With a Mentor Commented:
Just handle your error

On error goto ErrH

     blah blah blah
     blah blah blah

     Exit Sub

     If Err = 9 then 'Subscript out of range
          Msgbox "Some data is missing",vbInformation
          Msgbox Error,vbInformation 'Or your custom message
     end if

     Resume Fin

The UBound and LBound will return the upper and lower boundries of a given array.  So, if Ubound(MyArray) = LBound(MyArray) then it is empty.
Or try, UBound(MyArray) = 0 then there are no elements in the array.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

greendotAuthor Commented:
not if it's a single element array...

Plus, LBound and UBound return the "subscript out of range" exception as well.

I think I may just make a function call that tries to access LBound and if it fails then I know it's empty.
If Ubound(MyArray) = LBound(MyArray) then the array has a single element.
Dim ub, v As TCriteria

On Error Resume Next
ub = Ubound(v.Provider)
On Error Goto 0

If ub = -1 Then
  MsgBox "It's really empty"
  MsgBox "Thera are " & ub + 1 & "items in the array"
End If

Ture Magnusson
Karlstad, Sweden
greendotAuthor Commented:
I made the function:

Function IsArrayEmpty(va As Variant) As Boolean
   Dim i As Long
   On Error Resume Next
   i = LBound(va, 1)
   IsArrayEmpty = (Err <> 0)
   Err = 0
End Function

This seems to work fine.  If an array gets passed, it gets evaluated.
If anything else gets passed, it fails the LBound attempt.

Is there anything built-in to VB to do the same thing?

go on with your function..., its seems just fine
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.

All Courses

From novice to tech pro — start learning today.