Why use redim when creating an array of arrays?

J

Joe Dunfee

I am working on a project where I need to do an array of an array. I've done
some reason, and see that the general approach is to create the sub-arrays,
and then set each of the sub-arrays equal to an element in the parent array.
I've seen two approaches, one being in the thread titled "jagged arrays", and
the other using the redim command titled, "How do you create an Array of
Arrays?".

I understand the first method, but I don't understand why the redim command
is used in the other. It seems a bit more complex, but does seem to be the
prefered method in all the articles I've read about this subject. Is there
some benefit I will be missing if I don't use the redim method?

Joe Dunfee
 
B

Bob Phillips

Redim is normally used when you don't know the size of the target array in
advance, so you extend it (Redim Preserve) as you go along.

Without seeing the code, it is difficult to offer any more info.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
J

Joe Dunfee

Here is the code using redim I found in the "Collection vs. Array of Arrays"
thread.

'-------------------------------------
Sub TestCollectionAccess()
Dim col As VBA.Collection
Dim intArray() As Integer
Dim strarray() As String
Dim x As Long
Dim y As Long
Dim z As Variant
Dim zz As Variant

ReDim intArray(1 To 5, 1 To 5)
ReDim strarray(1 To 5, 1 To 5)

'Load the integer array
For x = 1 To 5
For y = 1 To 5
intArray(x, y) = x * y
Next
Next

'Get value
z = intArray(3, 4)

'Load the collection
Set col = New Collection
col.Add intArray
col.Add strarray

'Get value
zz = col(1)(3, 4)

MsgBox z & " and " & zz & " should be the same "
Set col = Nothing
End Sub
'-----------------------------


And here is another example [from
http://en.wikibooks.org/wiki/Programming:Visual_Basic_Classic/Arrays]

The real power of arrays comes when defining arrays of arrays. What does
this mean? Declare an array:

'-------------------------------
Dim varray() As Variant

varray = Array()
ReDim varray(1) As Variant
varray(0) = Array(1,2,3)
varray(1) = Array(4,5,6)
What do we have here? Essentially two arrays inside another. They can be
reference like this:
Debug.Print varray(0)(2)
'-----------------------------

Joe Dunfee
 
B

Bob Phillips

Seems superfluous here to me, could just as easily do

Dim intArray(1 To 5, 1 To 5) As Integer


Another reason for using ReDim is when you pass the array size to a
function, but again not applicable here.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
J

Joe Dunfee

That clarifies things a lot. For some reason, most of the examples I found
use the redim approach. I see the benefit of using redim to change the size
of the array, but it is not applicable in my case.

Thank You,
Joe Dunfee

Bob Phillips said:
Seems superfluous here to me, could just as easily do

Dim intArray(1 To 5, 1 To 5) As Integer


Another reason for using ReDim is when you pass the array size to a
function, but again not applicable here.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

Joe Dunfee said:
Here is the code using redim I found in the "Collection vs. Array of
Arrays"
thread.

'-------------------------------------
Sub TestCollectionAccess()
Dim col As VBA.Collection
Dim intArray() As Integer
Dim strarray() As String
Dim x As Long
Dim y As Long
Dim z As Variant
Dim zz As Variant

ReDim intArray(1 To 5, 1 To 5)
ReDim strarray(1 To 5, 1 To 5)

'Load the integer array
For x = 1 To 5
For y = 1 To 5
intArray(x, y) = x * y
Next
Next

'Get value
z = intArray(3, 4)

'Load the collection
Set col = New Collection
col.Add intArray
col.Add strarray

'Get value
zz = col(1)(3, 4)

MsgBox z & " and " & zz & " should be the same "
Set col = Nothing
End Sub
'-----------------------------


And here is another example [from
http://en.wikibooks.org/wiki/Programming:Visual_Basic_Classic/Arrays]

The real power of arrays comes when defining arrays of arrays. What does
this mean? Declare an array:

'-------------------------------
Dim varray() As Variant

varray = Array()
ReDim varray(1) As Variant
varray(0) = Array(1,2,3)
varray(1) = Array(4,5,6)
What do we have here? Essentially two arrays inside another. They can be
reference like this:
Debug.Print varray(0)(2)
'-----------------------------

Joe Dunfee
 
J

Joe Dunfee

As I got more into using the code, I ran into problems. The master-array
elements must set equal to the sub-arrays only AFTER the values are entered
for the sub-arrays. It seems that once you create the array-within-an-array,
the values of the elements are set and cannot be changed. Here is my sample
code with comments;

'==================
Sub ABC()
Dim vArr1(1 To 10) As Variant
Dim vArr2(1 To 5) As Variant
Dim vMainArr(1 To 2) As Variant
Dim lCounter As Long
Dim s As String, i As Long, j As Long

' Populate the sub arrays:
For lCounter = 1 To 10
vArr1(lCounter) = lCounter
If lCounter < 6 Then _
vArr2(lCounter) = lCounter * 2
Next lCounter

' Assign the sub arrays to the main array:
' Note that this must be done after the sub arrays are populated with values
vMainArr(1) = vArr1
vMainArr(2) = vArr2

'change one of the elements as a test
vArr2(2) = 100 ' note that this change WON'T show in the vMainArr array

' Show the results
s = ""
For i = 1 To 2
For j = LBound(vMainArr(i), 1) To UBound(vMainArr(i), 1)
s = s & vMainArr(i)(j) & ","
Next j
s = s & vbNewLine
Next i
MsgBox s

End Sub

'==================

Joe Dunfee
 

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