Results 1 to 21 of 21

Thread: [resolved for the moment] IsEmptyArray()?

  1. #1

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    [resolved for the moment] IsEmptyArray()?

    How can I get this to work?

    VB Code:
    1. Public Sub Foo(List() As Long)
    2.     If IsEmpty(List) Then
    3.         MsgBox "List is empty"
    4.     Else
    5.         MsgBox "List contains " & UBound(List) + 1 & " elements."
    6.     End If
    7. End Sub
    8.  
    9. Private Sub Form_Load()
    10.     Dim Temp() As Long
    11.    
    12.     'This would work but I need to check for EMPTY arrays
    13.     'ReDim Temp(2)
    14.    
    15.     Foo Temp
    16. End Sub

    Basically I need a function to check if a array contains any elements or not.
    Last edited by Fox; Mar 24th, 2004 at 02:13 PM.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    How about this...
    VB Code:
    1. Private Function IsArrayEmpty(SourceArray As Variant) As Boolean
    2. On Error Resume Next
    3.  
    4. Dim l As Long
    5.  
    6.     l = UBound(SourceArray) - LBound(SourceArray) + 1
    7.     IsArrayEmpty = (l > 0)
    8.  
    9. End Function
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Nah calling another function out of my function would result in too much performance loss. Besides, this would be shorter:
    VB Code:
    1. Public Function IsArray(List() As Long) As Boolean
    2. On Error Resume Next
    3.     IsArray = UBound(List) > -1
    4. End Function

    But that's not what I'm looking for.

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by Fox
    But that's not what I'm looking for.
    How could it do exactly what you asked, and yet not be what you are looking for? What is it you are looking for then?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    I'll offer some potentially useless advice here, but it's something I'm good at, so here it goes:

    VB Code:
    1. Private Sub cmdCheckArray_Click()
    2.  
    3.      Dim check As Boolean
    4.      Dim number As Integer
    5.  
    6.      check = False
    7.  
    8.      For number = 1 To Len(Array())
    9.           If Array(number) = "" Then
    10.                check = True
    11.           End If
    12.      Next number
    13.  
    14.      If Empty = True Then
    15.           MsgBox "Array Is Empty."
    16.      Else
    17.           MsgBox "Array contains " & Len(Array()) & " elements."
    18.      End If
    19.  
    20. End Sub

    I'm not in VB right now, so I'm not sure if it's the exact syntax, but it should be close.

  6. #6

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    crptcblade: I'm looking for a built-in function to check whether a array contains any elements or not.

    timeshifter: This won't work at all, you can't use Len() on arrays.

  7. #7
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    you're going to have to use On Error, or look at where the array is being defined/filled

  8. #8
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    One approach is to get the SAFEARRAY header and check the cDims member.
    an ending

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Public Sub Foo(List() As Long)
    2.     On Error Resume Next
    3.     MsgBox "List contains " & UBound(List) + 1 & " elements."
    4.     If Err <> 0 Then
    5.         MsgBox "List is empty"
    6.     End If
    7. End Sub

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by Fox
    crptcblade: I'm looking for a built-in function to check whether a array contains any elements or not.

    timeshifter: This won't work at all, you can't use Len() on arrays.

    Hrmmm.... UBound is built-in.... LBound is built-in.... WHAT's the problem?!?!?!?!!?

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by techgnome
    Hrmmm.... UBound is built-in.... LBound is built-in.... WHAT's the problem?!?!?!?!!?

    TG
    If you just do

    VB Code:
    1. Dim Temp() As Long
    2.    
    3.     Msgbox UBound(Temp)
    you get an error.

    Fox: There is no built in way.

  12. #12

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Okay thanks for the input. I'll try azteched's idea and compare the performance to a simple error catching.

    *bows*

  13. #13
    Addicted Member
    Join Date
    May 2001
    Location
    Montréal, Québec
    Posts
    195
    I may be wrong

    but for me foo is a "fonction orientée objet" so why not include variable in your object that keep track of the number of objects in your lists? Something like java does for the collections.
    (An object that keep track of the list object)

    When you add something to the list, add one to your variable
    and when you withdraw something, substract one from it.

    When you need to know how much object is in your list, you have only a variable tchek, which is something really fast.

    In other word, is a sort of a wrapper for your lists with some methods like add, remove and sizeOf

  14. #14
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    VB Code:
    1. Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long

    and

    VB Code:
    1. Dim ElementCount as Long
    2. Dim aArray () as Long
    3.  
    4.    ElementCount = SafeArrayGetDim(aArray)

    returns zero if array is empty otherwise returns non-zero.

  15. #15
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    yep, that does the cDims checking for you.
    an ending

  16. #16
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    if you're that worried about speed surely you'll want the On Error approach vs the api call

  17. #17

  18. #18
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    The api call may well be faster, because internally VB will check the dimensions when you call UBound anyway, that's where you get the error raised from. VB has to set the error number too. Either way, it's going to be a completely negligible difference as ML said.
    an ending

  19. #19

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Okay everyone, I made the speed test and here's the results. The only notable things were that the API is getting slower as the array size grows while the OnError method isn't affected by that. Instead OnError gives bad results if the array is empty.

    Code:
    // No Redim
    On Error:            7.98
    SafeArray:           0.16
    // Error Resume Next
    On Error:            8.12
    SafeArray:           0.15
    
    // Redim Temp(0)
    On Error:            0.52
    SafeArray:           2.74
    // Error Resume Next
    On Error:            0.51
    SafeArray:           2.73
    
    // Redim Temp(200)
    On Error:            0.51
    SafeArray:          19.19
    // Error Resume Next
    On Error:            0.53
    SafeArray:          17.18
    As you can see I made 3 test with different array sizes. The test looks like this:

    Code:
    'Test functions
    Public Function CheckArray_SafeArray(iArray() As String) As Boolean
        CheckArray_SafeArray = (SafeArrayGetDim(iArray) > 0)
    End Function
    
    Public Function CheckArray_OnError(List() As String) As Boolean
    On Error Resume Next
        CheckArray_OnError = UBound(List) > -1
    End Function
    
    'Caller sub
    Public Sub Main
        For A = 0 To Count: Temp = CheckArray_OnError(Foo): Next
        For A = 0 To Count: Temp = CheckArray_SafeArray(Foo): Next
    
    On Error Resume Next
        For A = 0 To Count: Temp = CheckArray_OnError(Foo): Next
        For A = 0 To Count: Temp = CheckArray_SafeArray(Foo): Next
    End Sub
    As you can see the 2nd calls are just with activated On Error in the caller sub - which is speeding up the code a very little.
    Last edited by Fox; Mar 24th, 2004 at 08:07 PM.

  20. #20
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    Strange.. I wouldn't have thought either method would vary in speed with size of array, especially the API, as I presume the SAGD function is a thin wrapper something like:

    Code:
    int SafeArrayGetDim(SAFEARRAY*s){
    return s->cDims;
    }
    an ending

  21. #21

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    There is probably some overhead for each data item when calling external DLL functions.. Couldn't expain this otherwise.

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