Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    counting array elements (vb6)

    Hello all

    I have an array with soem of it being empty after the data gets put into it, how can I count the non-empty elements in my array.

    Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: counting array elements (vb6)

    Use something like

    Dim lngCount As Long
    Dim lngIndex As Long
    lngCount = 0
    For lngIndex = LBound(arrMyArray) To UBound(arrArray)
    If Not IsEmpty(arrMyarray(lngIndex)) Then lngCount = lngCount + 1
    Next lngIndex
    MsgBox lngCount

    where arrMyArray is the name of the array (I have assumed it to be one-dimensional)

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: counting array elements (vb6)

    IsEmpty works only for Variants.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: counting array elements (vb6)

    That is correct. I had assumed that the poster was working with Variants, but it might be useful to explain this.

    Each array element is assigned a default value when the array is created.
    For an array of Variants, this default value is Empty.
    For an array of Integers or another numeric type, it is 0.
    For an array of Strings, it is "".
    For an array of Booleans, it is False.

    If you have an array of Integers or Strings or Booleans etc., you really can't say that an element is "empty". You can't distinguish between an element that is 0 because it was never assigned a value by the user/programmer, and an element that is 0 because the user/programmer set it to 0 explicitly.

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: counting array elements (vb6)

    It may of interest to note how differently VB and VB .NET handle "Nothing".
    The following VB 6 code is invalid, but it imports "correctly" into VB .NET.

    <pre>Option Explicit
    Private Sub Main()
    TestNothing
    End Sub
    Private Sub TestNothing()
    Dim intIndex As Integer
    intIndex = -1
    intIndex = Nothing
    Debug.Print intIndex
    End Sub
    </pre>



    In VB 6, the code is incorrect because you cannot set an Integer variable to Nothing.

    When converted to VB .NET, you can use such an assignmenbty, but Nothing, in VB .NET, means the default value of 0, so you still cannot detect a variable that has not been assigned.

    The VB .NET code is:

    <pre>Option Strict Off
    Option Explicit On
    Module modMiscellaneous
    Public Sub Main()
    TestNothing()
    End Sub
    Private Sub TestNothing()
    Dim intIndex As Short
    intIndex = -1
    intIndex = Nothing
    System.Diagnostics.Debug.WriteLine(intIndex)
    End Sub
    End Module
    </pre>


    NOTE: Is there a bug in the use of the pre tag in the lounge, or did I not use the pre tag correctly? There definitely is a problem with extra blank lines being inserted after the each /pre tag.

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: counting array elements (vb6)

    The <!t>[Pre]<!/t> and <!t>[Quote]<!/t> tags both insert blank lines before and after to set the enclosed text off from the rest of the message. Just don't stick any additional blank lines in and it will be fine. That's the way they've worked from the beginning.
    Charlotte

Posting Permissions

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