Results 1 to 10 of 10
  1. #1
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    IsEmpty? Not (Office 2K)

    If I have a dynamic array, b(), of objects, blotz, declared as New:
    <pre>Dim b() As New blotz</pre>

    how can I tell if the array is empty?

    When it is empty
    IsEmpty([img]/forums/images/smilies/cool.gif[/img] returms False, I guess because of the new, but
    UBound([img]/forums/images/smilies/cool.gif[/img] gives an error!

    Currently, I just use On Error, but I hate that! Is there a better way? TIA --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  2. #2
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IsEmpty? Not (Office 2K)

    in my reading, i think it is of Getz's material (?), i have found that the writers recommend writing two lines to make an instance rather than one. This seems reasonable to me. I would therefore write the code whilst observing that Isempty applies to variants:
    <pre>Public Function f()
    Dim b As Variant
    If IsEmpty([img]/forums/images/smilies/cool.gif[/img] Then
    Debug.Print "is empty"
    Else
    Debug.Print "aint"
    End If
    If IsArray([img]/forums/images/smilies/cool.gif[/img] Then
    Debug.Print UBound([img]/forums/images/smilies/cool.gif[/img]
    Else
    Debug.Print "not an array (yet)"
    End If
    End Function

    Public Sub run()
    f
    End Sub</pre>


  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IsEmpty? Not (Office 2K)

    You could try this:

    <pre> Dim iCnt As Integer
    iCnt = 0
    On Error Resume Next
    iCnt = UBound([img]/forums/images/smilies/cool.gif[/img]
    On Error GoTo 0
    If iCnt = 0 Then
    MsgBox "Its empty."
    End If
    </pre>

    Legare Coleman

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: IsEmpty? Not (Office 2K)

    Well, that is bizzare! I made a little test sheet similar to your code, but created a class blotz and dim'ed b as "Dim b() As New blotz". Code worked great, when b is empty, its lower-bound is 0 and its upper lownd is -1. Cool. But, I thought UBound([img]/forums/images/smilies/cool.gif[/img] gave me an error. Well, not anymore, I moved the code over to the book where I wanted it and it works fine. However, when I tried to make a general purpose notEmpty function, I could not tell the difference between a variant that was equal to my b array and a variant the was equal to an integer. They both came back empty! Here is the code:
    <pre>Public Function notEmpty(v As Variant) As Boolean
    If IsEmpty(v) Then
    notEmpty = False
    Else
    If IsArray([img]/forums/images/smilies/cool.gif[/img] Then
    If UBound([img]/forums/images/smilies/cool.gif[/img] < LBound([img]/forums/images/smilies/cool.gif[/img] Then
    notEmpty = False
    Else
    notEmpty = True
    End If
    Else
    notEmpty = True
    End If
    End If
    End Function</pre>


    Don't worry too much about it. I only need it for non-variants, it works great on them. Thanks! --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: IsEmpty? Not (Office 2K)

    Well, Legare, I looked at your code and said "He's crazy! It dosn't give an error!" (Of course, I didn't test it!) About 2 minutes later as I was single-stepping through my code, which still looks like yours, I got the error, so after eating my words, I did some more exploring. This is what's happening: initially, UBound([img]/forums/images/smilies/cool.gif[/img] gives an error, but as soon as you use Lbound([img]/forums/images/smilies/cool.gif[/img] (which returns 0), then UBound([img]/forums/images/smilies/cool.gif[/img] returns -1. Bizzare, but consistent. I still would like a general solution that does not use On Error.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  6. #6
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IsEmpty? Not (Office 2K)

    so therefore, i presume that you will write
    lower = LBound([img]/forums/images/smilies/cool.gif[/img]
    to 'set' the dimensions, and then use
    upper = UBound([img]/forums/images/smilies/cool.gif[/img]
    for your test?

  7. #7
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: IsEmpty? Not (Office 2K)

    Actually, I'm still totally flummouxed: every time that I single-step through my code, it seems like something different happens. Don't believe any of my posts. I just put the initialiazation code between on-error's for now: got too much code to write to fix it right:
    <pre> On Error Resume Next ' In case it's already empty
    For i = LBound(csv) To UBound(csv)
    csv(i).Kill
    Next i
    On Error GoTo 0</pre>

    where csv is a dynamic array of objects dim'ed as a class that looks like a csv file and/or worksheet. I just want to make sure that there is nothing around when I start over and this does it. If csv is "empty," then the on error fires several times and the code between the on-error statments does nothing.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  8. #8
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Faifax, Virginia, USA
    Posts
    542
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: IsEmpty? Not (Office 2K)

    "If you don't have time to do it right, now, you WILL have time to do it over." This adage demonstrates that programmers have already invented time travel, but only to the past.

  9. #9
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: IsEmpty? Not (Office 2K)

    <img src=/S/yep.gif border=0 alt=yep width=15 height=15>, but it IS right, it just ain't pretty nor reusable! <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  10. #10
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Marietta, Georgia, USA
    Posts
    296
    Thanks
    9
    Thanked 4 Times in 4 Posts

    Re: IsEmpty? Not (Office 2K)

    Sam, the way the code is acting is a side effect of the variable declaration using "Dim b() As New blotz". If you use "Dim As New" syntax, the VBA engine inserts extra code in front of EACH line of your code. If the variable is being referenced by your code for the FIRST time, then the hidden code instantiates the variable.

    To avoid this problem, it is recommended that you always separate the declaration of the variable from its instantiation. This allows you to control exactly when the instantiation occurs. It also makes your code work the way you want it to, without the interaction of the "hidden code":

    Dim b() As blotz 'This simply defines the type of the variable b

    'Later on
    Set b = New blotz 'This assigns memory and instantiates the variable
    Rick Groszkiewicz
    Life is too short to drink bad wine (or bad coffee!)

Posting Permissions

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