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
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » 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)
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.
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.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications