ArrayHasElements - anything better than this?



  • I found this code, but not dealing with VB a lot, I don't know if it's a WTF or valid. Could you help me?

    Thanks, Adam



    Public Function ArrayHasElements(vArray As Variant) As Boolean

        Dim iArraySize As Integer



        On Error GoTo ArrayHasElements_Error



        iArraySize% = UBound(vArray)

        ArrayHasElements = True

        Exit Function

        

    ArrayHasElements_Error:

        ArrayHasElements = False

        Exit Function



    End Function



  • Re: ArrayHasElements - anything better than this?

    Try passing vArray as an empty array...

    I guess... UBound will return 0 and say it has elements...

    Not sure.. have to test first...

    Need something like:

    iArraySize = UBound(vArray)

    if iArraySize >0 then

    ArrayHasElements = True

    else

    ArrayHasElements = False

    end if

     

    @aakoch said:


    Public Function ArrayHasElements(vArray As Variant) As Boolean
        Dim iArraySize As Integer

        On Error GoTo ArrayHasElements_Error

        iArraySize% = UBound(vArray)
        ArrayHasElements = True
        Exit Function
        
    ArrayHasElements_Error:
        ArrayHasElements = False
        Exit Function

    End Function



  • VB doesn't have

    Len(theArray) > 0?


    Like its strings do?

    I learned a new word from my cousing who is in CS now:

    orthogonality



  • @XoK said:

    Try passing vArray as an empty array...

    I guess... UBound will return 0 and say it has elements...

    If you pass it an empty array (e.g. Dim a() as integer) then UBound will throw an error, thus the need for the On Error. Also, since the vArray parameter is a Variant, the On Error would handle a situation where something other then an array gets passed to this function (which would be a WTF in and of itself). Can't think of any simpler way to do this in VB6.

    Dan



  • Re: ArrayHasElements - anything better than this?

    Here's what I've used for years.  The comparison between UBound & LBound is the key.  If its empty then LBound is the greater of the two.  Of course, you have to check that it is an array 1st.

    Public Function aEmpty(aArray As Variant) As Boolean
      On Error GoTo GetOut
      aEmpty = True
      If IsArray(aArray) Then
        If LBound(aArray) <= UBound(aArray) Then
          aEmpty = False
        End If
      End If
    GetOut:
    End Function


Log in to reply