Results 1 to 30 of 30

Thread: VB6 - Returning/Detecting Empty Arrays

Threaded View

  1. #1

    Thread Starter
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    VB6 - Returning/Detecting Empty Arrays

    Lots of times you need to create a function, property, method, etc. that returns an array. But there are cases where you have "nothing to return" and you'd like to return an "empty" array.

    Sometimes an empty array result might be a valid condition. Or maybe you want to use it to signal an error condition of some kind.


    The usual approaches require that the caller rely on exception handling to detect this "emptiness" but at least for Byte arrays and Variant arrays there are a couple of tricks you can use:

    Code:
    Option Explicit
    
    Private B() As Byte
    Private V() As Variant
    
    '---------------------------------------------------------------
    'Creating empty arrays.  Just do this inline in real programs,
    'you don't need these functions:
    
    Private Function MakeEmptyB() As Byte()
        MakeEmptyB = ""
    End Function
    
    Private Function MakeEmptyV() As Variant()
        MakeEmptyV = Array()
    End Function
    
    '---------------------------------------------------------------
    'Testing for empty arrays.  Just do this inline in real programs,
    'you don't need these functions:
    
    Private Function IsEmptyB(ByRef Bytes() As Byte) As Boolean
        IsEmptyB = UBound(Bytes) < LBound(Bytes)
    End Function
    
    Private Function IsEmptyV(ByRef Variants() As Variant) As Boolean
        IsEmptyV = UBound(Variants) < LBound(Variants)
    End Function
    
    '---------------------------------------------------------------
    'Do tests with error trapping, printing results on the Form:
    
    Private Sub PerformTests()
        On Error Resume Next
        Print IsEmptyB(B)
        If Err Then Print Err.Number, Err.Description
        Err.Clear
        Print IsEmptyV(V)
        If Err Then Print Err.Number, Err.Description
        On Error GoTo 0
    End Sub
    
    Private Sub Form_Load()
        AutoRedraw = True
    
        'Doesn't work (subscript out of range errors):
        Erase B
        Erase V
        PerformTests
    
        Print
    
        'Works:
        B = MakeEmptyB()
        V = MakeEmptyV()
        PerformTests
    End Sub
    If the arrays you would normally return would have bounds greater than or equal to zero (0) then you can simplify the "tests" by checking for UBound = -1, which is what these approaches (both Byte and Variant arrays) will return.

    As suggested in the comments in the code above, it usually isn't worth writing these functions. Just use the one-line code inline where you need it.


    If there is an equivalent for other array types I haven't found it. Perhaps you know of a trick for Long, Single, UDT, or other types and can post it here.
    Last edited by dilettante; Sep 28th, 2013 at 04:47 PM. Reason: typo corrected

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width