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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Used Array Count

Status
Not open for further replies.

segmentationfault

Programmer
Jun 21, 2001
160
US
Suppose I have
Code:
 Dim AllNumbers(50) As Double
and I only use the first 15 doubles. If I pass this array to another method, is there a way to find out how many of these doubles have been initialized rather than just declared aside from iterating through them? If I have an array of strings, is there a way to find out how many are not null strings without looking at each one?
 
Why not create a dynamic array and then use Ubound(array) to find the number of items ??

From VBA help:
Declaring a Dynamic Array

By declaring a dynamic array, you can size the array while the code is running. Use a Static, Dim, Private, or Public statement to declare an array, leaving the parentheses empty, as shown in the following example.

Dim sngArray() As Single

Note You can use the ReDim statement to declare an array implicitly within a procedure. Be careful not to misspell the name of the array when you use the ReDim statement. Even if the Option Explicit statement is included in the module, a second array will be created.

In a procedure within the array's scope, use the ReDim statement to change the number of dimensions, to define the number of elements, and to define the upper and lower bounds for each dimension. You can use the ReDim statement to change the dynamic array as often as necessary. However, each time you do this, the existing values in the array are lost. Use ReDim Preserve to expand an array while preserving existing values in the array. For example, the following statement enlarges the array varArray by 10 elements without losing the current values of the original elements.

ReDim Preserve varArray(UBound(varArray) + 10)

HTH
~Geoff~
[noevil]
 
That's a great idea, but not a perfect solution.

As I'm reading input from an Excel worksheet, I would either have to count my inputs before sizing the dynamic array (nothing gained) or I would have to resize the array every time I add to it (time lost?*). If I resize it with a traditional doubling scheme or just make it 10 larger, as in the example, then I lose what I tried to gain - an array that is perfectly filled.

Probably the most compact solution would be to declare a large array and reserve 0 to hold the number of filled elements.

* I'll time study this later today and report the results. Maybe resizing the array every time I want to add to it is actually sufficiently fast to justify the menial convenience to the programmer.
 
I would've thought that doing a count before creating the array, so you know how many are in there would be the most sensible (and least time consuming) option - what are you proposing to do with the array (and its items) as this may well impact the method you use ?? HTH
~Geoff~
[noevil]
 
Well, if I count them before I size the array, then I can use the UBound() function to find out how many there are later. Alternatively, I can dump everything into a large array and count them later. I think it's obviously a better programming practice to use the dynamic array approach, but I'm not yet compelled to change my existing code.

I'm just passing an array to a method and don't want to waste the time to count the items. If I was passing the array to more than one method, then time would be saved by only counting once, but I'm currently only counting once anyway...
 
hey segmentationfault,

xlbo has given you a fine helpful and expert response. Dosn't he deserve a STAR? Skip,
SkipAndMary1017@mindspring.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top