Choosing one of several arrays?

E

Ed

I have strings belonging to different series - A, B, C, and D - that I want
to write to a corresponding array - aryA(), aryB(), aryC(), or aryD(). I
will ReDim each array before I write the next string using a variable "x" to
represent the next number. What is the right way to refer to the correct
array? I've tried the following with no luck
-- Assign a variable (such as "Y") to the correct series and use
ReDim ary & Y(x)
-- Assign a variable to whole array name and use
ReDim Y(x)
-- Assign a variable to whole array name and use
ReDim Array("Y")(x)

Any help?
Ed
 
J

Jezebel

You can't do any tricks with variable names. In the compiled code -- which
is what actually runs -- there are no variable names at all; only pointers.

Two workarounds that might suit (depending on what else is going on in your
code) --

1) Use an array of arrays:

Dim pData(1 to 4) as variant

Dim aryA() as string
Dim aryB() as string
:

pData(1) = aryA
pData(2) = aryB

It's maybe a little obfuscated, but you can refer to aryA(3) using
pData(1)(3)


2) Use a collection:

Dim pCollection as collection
Dim aryA() as string
Dim aryB() as string

set pCollection = new collection
pCollection.Add Item:=aryA, Key:="Array_A"
 
E

Ed

Thanks, Jezebel. Thinking a bit more, I wonder if it's possible and, if so,
maybe easier to use a two-dimensional array and use a variable to decide
where I'm writing. For instance, if I have six series of data, can I write
to MyAry(x, y), where "x" is assigned to 1-6 according to which series I
assign my data to?

Also, if I use ReDim Preserve with this, I know I can only resize "y". Is
"y" then the total number of elements across the whole complete array, or
just that section of the array? Or do I ReDim to the highest number of
elements in any single section? If in MyAry(1,y) y=4 and in MyAry(2,y) y=6,
if I wanted to add another item to MyAry(1,y+1) would I have to ReDim to 5,
or to 11, or not have to ReDim at all because MyAry(2,y) has 6 items, more
than the 5 the first section will have? (Did that make sense??)

Ed
 
J

Jezebel

Yes you can use a two-dimensional (or indeed any dimensional) array; but the
dimensions are constant (think of the array as representing a rectangle -
or cube or whatever - of cells). So you'd have to set it up for the second
dimension to accommodate the largest set of values you might need.
 

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