Link to home
Start Free TrialLog in
Avatar of greendot
greendot

asked on

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?

thanks,
j
Avatar of jjmartin
jjmartin

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.
Avatar of greendot

ASKER

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

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

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

Ture Magnusson
Karlstad, Sweden
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
ASKER CERTIFIED SOLUTION
Avatar of jcastr
jcastr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial