Results 1 to 30 of 30

Thread: VB6 - Returning/Detecting Empty Arrays

Threaded View

  1. #13
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: VB6 - Returning/Detecting Empty Arrays

    Quote Originally Posted by dilettante View Post
    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.
    A very good and comprehensive discussion how to determine an EMPTY array. I followed all of replies but none of them give confirmed flawless solution.

    My case:
    Code:
    Public Type udtMapPos
      Row1 As Long
      Col1 As Long
      X1 As Long
      Y1 As Long
    End Type
    private MapPos() As udtMapPos
    What I should use to determine empty MapPos ,either dilettante,olaf or wqweto?

    Edited:
    I decided to use vbAccelerator method:
    Code:
    Private Function ArrayCheck(ByRef vArray As Variant) As Boolean
    
    '/* validity test
    
        On Error Resume Next
    
            '/* an array
            If Not IsArray(vArray) Then
                GoTo Handler
                '/* not dimensioned
            ElseIf IsError(UBound(vArray)) Then
                GoTo Handler
                '/* no members
            ElseIf (UBound(vArray) = -1) Then
                GoTo Handler
            End If
            ArrayCheck = True
    
    Handler:
        On Error GoTo 0
    
    End Function
    Last edited by Jonney; Dec 7th, 2014 at 10:27 PM.

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