Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Why use redim when creating an array of arrays?

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Why use redim when creating an array of arrays?

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)

"Joe Dunfee" wrote in message
...
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Why use redim when creating an array of arrays?

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/Program...lassic/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
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Why use redim when creating an array of arrays?

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" wrote in message
...
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/Program...lassic/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



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Why use redim when creating an array of arrays?

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" wrote:

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" wrote in message
...
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/Program...lassic/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






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Why use redim when creating an array of arrays?

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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ReDim Array Brandt Excel Programming 3 October 9th 06 06:42 PM
ReDim Array Viktor Ygdorff Excel Programming 2 July 10th 06 04:04 PM
Creating a single vertical array from multiple column arrays Bryan Excel Worksheet Functions 2 December 10th 05 07:12 PM
ReDim Object array as parameter of Variant array Peter T Excel Programming 4 May 10th 05 02:11 PM
ReDim, Preserve and Multidimensional arrays Andy Westlake[_2_] Excel Programming 3 October 19th 04 07:04 PM


All times are GMT +1. The time now is 05:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"