Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting total no. of elements in an array!

Status
Not open for further replies.

softjack

Programmer
Dec 4, 2005
44
IN
Hi
i want to get the total no. of elements stored in an array.

example:
suppose i store 3 values out of 5(UBound) in an array

how can i get this value (3)

Can somebody please help!
 
Hi softjack,

Basically you can't - not without walking the array and counting them, anyway.

If the array is of Variants then those that have not had values stored in them will be Empty - although they could also be set to Empty after having been used, I suppose.

If the array is of typed variables they will have been initialised and there is no difference between, say, a default zero in a Long and an explicitly stored value of zero in the same Long.

In your own application there may be all sorts of specific criteria you can use - only you will know that. One way, perhaps would be to count them as you store values in them - again only you know the data and the process.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
You may want to consider using a Collection instead of an array, which has a built-in Count property. Also take a look at the Dictionary object, part of the Microsoft Scripting Runtime. (Manipulating a collection is inherently slower than an array, however.)

If you stick with an array, you could, perhaps, reserve the first element for the count, depending on the data type you've Dim'd the array to be (or maintain the count in a separate variable as Tony suggests). Lastly, why not resize the array when you populate/depopulate it? In that case, the UBound & LBound functions will always tell you the number of elements.


Regards,
Mike
 


If you need to use an array, consider ReDim Preserve
Code:
sub SomeSub()
   dim myArray() 
    i = 0
    for k = 1 to SomeLimit
      ' do stuff
      if (SomeExpression) then
        redim Preserve myArray(i)
        myArray(i)= SomeValue
        i = i + 1
      end if
    next k
end sub

Skip,

[glasses] [red]Be Advised![/red]
The band of elderly oriental musicians, known as Ground Cover, is, in reality...
Asian Jasmine![tongue]
 
For a variant array, you could 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 (assumed to be one-dimensional). IsEmpty works only for variants, because each array element is assigned a default value when the array is created.For an array of:
· Variants, this default value is Empty.
· Integers or another numeric type, it is 0.
· Strings, it is "".
· Booleans, it is False.
If you have an array of Integers, Strings or Booleans, the array elements aren't ever truly "empty". Taking an integer array, for example, you can't distinguish between an element that is 0 because it was never assigned a value, and an element that is 0 because it was set to 0 explicitly.

Redimming, as Skip suggests, is one way of making sure arrays of Integers, Strings and Booleans only hold meaningful elements. The trick, though, is to remove elements that are no longer is use - in the correct order. Unfortunately, you can't just pop an element out of the middle of the array - you'd need to shuffle all the other elements down (adjusting any pointers), then redim away the top-most element.

Cheers
 
Just to count or get values of items of an array, 'For Each' looping can be used. This allows to avoid nested loops, but the price is that array items are read-only and there is no access to subscripts:

Code:
Dim iArray(3, 5) As Integer
counter = 0
iArray(2, 3) = 1
For Each x In iArray
    If x > 0 Then counter = counter + 1
Next x
MsgBox counter

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top