VB dynamic arrays as lists

G

Guest

Hello all,

I want to perform the following task:

1. Create an array
2. Fill it up (number of elements unknown in advance)
3. Iterate through it using For Each loop (cannot do this in the in the
previous step)

I ended up with something like this:

1 Dim arr()
2 ReDim arr(0)
3 Do While <Loop Condition>
4 'Data acquisition here
5 ReDim Preserve arr(UBound(arr,1) + 1)
6 arr(UBound(arr,1) - 1) = theData
7 Loop
8 ReDim Preserve arr(UBound(arr,1) -1)
...
9 For Each item In arr
10 'First data processing here
11 Next
...
12 For Each item In arr
13 'Second data processing here
14 Next

What I don't like:
- Have to redim every time a data is added to the array (line 5)
- Must have a dummy element (line 2) to make UBound happy
- Must drop the dummy element to make my for each simpler (line 8)
- Indices are ugly (lines 5, 6, and 8)

Could someone propose a better solution?
 
H

Herfried K. Wagner [MVP]

mom_newbie said:
I want to perform the following task:

1. Create an array
2. Fill it up (number of elements unknown in advance)
3. Iterate through it using For Each loop (cannot do this in the in the
previous step)

I suggest to use an 'ArrayList' object instead of the array. 'ReDim
Preserve' is a costly operation that should be avoided.
 
T

Tom Shelton

Hello all,

I want to perform the following task:

1. Create an array
2. Fill it up (number of elements unknown in advance)
3. Iterate through it using For Each loop (cannot do this in the in the
previous step)

I ended up with something like this:

1 Dim arr()
2 ReDim arr(0)
3 Do While <Loop Condition>
4 'Data acquisition here
5 ReDim Preserve arr(UBound(arr,1) + 1)
6 arr(UBound(arr,1) - 1) = theData
7 Loop
8 ReDim Preserve arr(UBound(arr,1) -1)
...
9 For Each item In arr
10 'First data processing here
11 Next
...
12 For Each item In arr
13 'Second data processing here
14 Next

What I don't like:
- Have to redim every time a data is added to the array (line 5)
- Must have a dummy element (line 2) to make UBound happy
- Must drop the dummy element to make my for each simpler (line 8)
- Indices are ugly (lines 5, 6, and 8)

Could someone propose a better solution?

Well... You have a couple of choices here.

1. Use an ArrayList
2. Use an Array

There are pro's and con's to both of these choices.

With the arraylist, you don't need to worry about the number of elements.
The ArrayList will grow as needed. The disadvantages to this approch is
that ArrayList stores it's elements as type System.Object. That means that
if you are storing value types (structures, integers, etc.) in the
ArrayList, then you have to deal with the performance hit of
boxing/unboxing on storage and retrival. This can be quite significant
as the list gets large. Where that cutoff is depends on your system, so
you may want to test this method and see if the performance is adequate.
If your types are Reference types (classes), then this not as big an
issue - but you do have to cast your objects back to the correct type on
retrieval. This is largely a non-issue in VB.NET 2005, with the
inclusion of generics.

With arrays, you have the issue of being limited to a fixed size. In
other words, you have to know up front how many elements you will store,
the tradeoff is that you will get much better performance when working
with large numbers of Value types. That said, there are ways around the
fixed size issue - using ReDim Preserve - and still maintain decent
performance. The cost is complexity. The way really to use arrays in
your above example is to NOT ReDim on every addition. The normal way is
to grow the array when ever you hit the number of elements. This is
actually the way arraylist works internally. It starts with enough
storage to hold 16 elements, but when you add the 17, it will basically
do a redim preserve on it's array and double the current storage. Of
course, then you have to have a way to keep track of the last element
you inserted etc. This would probably be best done inside of a custom
typed collection.

As stated, the methods you chose really depend on
1) the number of likely elements
2) the types of the data you are storing (Value vs. Reference types)

The only way to really know is to implement it, and see. Personally, I
would use the ArrayList first - and if you have any speed issues,
profile the app. If you find that it is your bottleneck, then you can
most likely speed things up with a custom collection implementation,
using an array that INTELLIGENTLY resizes as needed.
 
M

Matt

I'm sorry if this is insulting, but its like 'arrays as lists' = arraylist

this is just so ironic rofl
 
G

Guest

Obviously I posted this question to the wrong newsgroup.
I am writing a VBScript, not a VB program. Sorry.
Thanks everybody for the suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top