• counting array elements (vb6)

    Author
    Topic
    #375301

    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

    Viewing 0 reply threads
    Author
    Replies
    • #610322

      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)

      • #610727

        IsEmpty works only for Variants.

        • #610943

          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.

          • #611046

            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.

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

            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:

            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
            

            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.

            • #611093

              The [Pre] and [Quote] 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.

    Viewing 0 reply threads
    Reply To: counting array elements (vb6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: