Dynamic Array

C

Craig

I have the following code...I was told by some people on
this message board to use this 'ReDim' stuff or something
that looks like the following code to "pigeonhole", or
allocate memory to my array. My question is, what does
the following code do? I only want an array as big as how
many elements are going into it, and that number can
change each time. What am I actually doing in the below
code. Any help would be appreciated. thanks

Craig

Dim TheArray()
Dim n As Integer 'integer used with dynamic array
Dim tbl1 As DAO.Recordset

ReDim Preserve TheArray(0 To 9000) As String
For n = 0 To 9000
TheArray(n) = Chr(Int(Rnd * 26) + 65)
Next
ReDim Preserve TheArray(0 To 10000) As String
For n = 9001 To 10000
TheArray(n) = Chr(Int(Rnd * 26) + 65)
Next
 
P

Paul Overway

You are REDIMensioning an array with 9001 elements and then again with 10001
elements. You are assigning a random character to each element of the
array. You may want to do something like this instead:

Dim iNewSize as Integer
Dim iOldSize As Integer

iOldSize = UBound(TheArray)
iNewSize = 20000

ReDim Preserve TheArray(0 To iNewSize) As String
For n = iOldSize + 1 To iNewSize 'assign new value for the NEW
elements only
TheArray(n) = Chr(Int(Rnd * 26) + 65)
Next

Note that the use of Preserve retains the array's existing contents, but in
your routine...you overwrite the entire array anyway. So, there isn't much
point in using Preserve unless you do something like what is shown above.
 
S

solex

Craig
My question is, what does the following code do?

This is a good question but a better question is what is the following code
supposed to do?

It is surprising that this code works as it contains a syntax error. You
cannot redim an array and at the same time change the base datatype of the
array, the code should be:

Dim TheArray() As String
Dim n As Integer 'integer used with dynamic array

ReDim Preserve TheArray(0 To 9000)

For n = 0 To 9000
TheArray(n) = Chr(Int(Rnd * 26) + 65)
Next

ReDim Preserve TheArray(0 To 10000)
For n = 9001 To 10000
TheArray(n) = Chr(Int(Rnd * 26) + 65)
Next

The code (in an infficent way) fills a 10001 element array with random
capital letters. I would have written it as such:

Dim TheArray(0 To 10000) As String
Dim n As Integer 'integer used with dynamic array

For n = 0 To 10000
TheArray(n) = Chr(Int(Rnd * 26) + 65)
Next

There is no need to redim the array if you know exactly how many elements
you need at compile time.

Dan
 
J

Jonathan Parminter

-----Original Message-----
I have the following code...I was told by some people on
this message board to use this 'ReDim' stuff or something
that looks like the following code to "pigeonhole", or
allocate memory to my array. My question is, what does
the following code do? I only want an array as big as how
many elements are going into it, and that number can
change each time. thanks

Craig

Hi Craig,
to create an array that grows to accommadate an un
predictable number of items, use the following as an
example

**** start code ****

dim astrStuff() as string
dim intIndex as integer
dim intLoop as integer

' if array is declared at module or global level
' clear array
redim astrstuff(0)

for intloop =0 to ???
resize to accomodate new item
redim preserve astruff(intIndex)
astrstuff(intindex)="idea" & intindex
intindex=intindex+1
' allow for miscellaneous window activities
doevents
next intloop

for intindex=lbound(astrstuff()) to ubound(astrstuff())
debug.print astrstuff(intindex)
doevents
next intindex

***** end code ****

to use online help, click on keyword then use F1 key.

luck
Jonathan
 

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