Dynamic multi-dimensional array

D

DSH

Is it possible to create a dynamic multi-dimensional array? I know the
following will not work, but something to the effect:

dim arr()

dim s as string

s = "5, 6, 10"

redim arr(s) 'i.e. redim arr(5, 6, 10)

Thanks.
 
D

Dave Peterson

Dim Arr as variant
arr = array(5,6,10)


Is it possible to create a dynamic multi-dimensional array? I know the
following will not work, but something to the effect:

dim arr()

dim s as string

s = "5, 6, 10"

redim arr(s) 'i.e. redim arr(5, 6, 10)

Thanks.
 
R

Ryan H

Is this what you are wanting? Hope this helps! If so, let me know, click
"YES" below.

dim arr()

dim s as string

s = "5, 6, 10"

arr = Split(s, ",")
 
R

Rick Rothstein

I think the only way you will be able to do what you want is this way...

Dim arr(), Temp() As String
Dim s As String
s = "5, 6, 10"
Temp = Split(s, ",")
Select Case UBound(Temp)
Case 0 ' One dimensional array
ReDim arr(Temp(0))
Case 1 ' Two dimensional array
ReDim arr(Temp(0), Temp(1))
Case 2 ' Three dimensional array
ReDim arr(Temp(0), Temp(1), Temp(2))
Case <<etc.>> ' Keep this structure going depending on the maximum needed
......
End Select
 
D

DSH

No, sorry. I didn't state my original question well. I need an easy way to
change the number of dimensions of an array based on the changing value of
the number of dimensions and elements.

Here's the long way to do it:

numberOfdimensions = n

d(0) = 5
d(1) = 6
d(2) = 10

select case numberOfdimensions:

case 1:
redim arr(d(0))
case 2:
redim arr(d(0), d(1))
case 3:
redim arr(d(0), d(1), d(2))

etc. etc

end select

***

I'm looking for a simpler way. The following won't work, but is there a
similar way to do this:

for ddx = 0 to 2
s = s & d(ddx) & ", " ' s will end up = "5, 6, 10"
next ddx

redim arr(s)

Thanks again.
 
D

DSH

I need to dynamically set the number of dimensions and elements, not the
values of the elements. The dimensions and elements are dynamic, i.e. they
change. So I need a way programmatically to easily change these. arr() may
be arr(5, 6, 10) or arr(1, 2, 9, 4), etc. etc.
 

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