Add to array

pnklphnts
09-04-2003, 10:06 PM
I want to create an array and then add another thing to it. Thanks in advance.

Diurnal
09-04-2003, 10:20 PM
I want to create an array and then add another thing to it. Thanks in advance.

Option Explicit
Private Variable() as String

Form1_Load
'Initially Dimension the Array
Redim Variable(0 to 1)
Variable(0) = "One"
Variable(1) = "Two"
End Sub

Command1_Click
'Add one more element to the array.
Redim Preserve Variable(LBound(Variable) to UBound(Variable) + 1)
Variable(2) = "Three"
End Sub


Start by declaring your variable as an array, then dimension it and fill it with data. Later in your program, you can redimension it while preserving the data and add more data to it. This will get you started. Check MSDN for info on the statements.

Van^
09-05-2003, 08:08 AM
Using a collection would probably fit your needs better. If the list of items is dynamic, the collection will work better. Redimming arrays can be expensive if done often and the arrays are large.

--Van^

Thinker
09-05-2003, 08:20 AM
Of course Collections are expensive too if they have a large number of
items.

Van^
09-05-2003, 08:24 AM
Agreed, if used improperly. If you are going to be added items constantly, though, it is less expensive than creating bigger arrays each time. A Redim Preserve, from what I understand, simply allocates a new and bigger array. At that point, it memcpy's the data into the new array and destroys the old one. Since a collection is linked list based (from what I understand) this doesn't happen when you add items. It does have more initial overhead though.

--Van^

Thinker
09-05-2003, 08:30 AM
Very correct. Which is why if I know I am going to be redimming an
array a large number of times adding 1 each time, I instead redim less
often and add a larger number of new elements each time (usually based
on some growth percentage). That is still much faster than a Collection
once you get past, say 10000 items.

pnklphnts
09-05-2003, 06:07 PM
thanks alot ill try it

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum