Changing values of an array of arrays

J

Joe Dunfee

I am attempting to use an array of arrays (ragged arrays), but am having
problems.

It seems the master-array elements must set equal to the sub-arrays only
AFTER the values are entered for the sub-arrays. Then it seems the values of
the elements are set and cannot be changed. Here is my sample
code with comments (my next question follows);

'==================
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 if this isn't done after are populated with values, the array
will be empty.
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

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

If my issues above are true, then I need to figure out a way around these
limitations. Note that my actual program would have perhaps 200 elements in
the main array, and 20 sub elements.of mixed type (strings and numbers), then
about 5 of the sub-sub elements would be 1-dimensional arrays. Just using 200
separate arrays would not be desirable.

Perhaps I must repeat the lines where sub arrays are assigned to the main
array EVERY time I change a value.

Any other suggestions?

Joe Dunfee
 
T

Tom Ogilvy

You are correct that vArr1 and vArr2 have no relation to the array of arrays.
The MainArr(2) does not hold a pointer to vArr2. So if you want to change
the value in the MainArr(2), second position, then do it directly

'==================
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

vMainArr(1) = vArr1
vMainArr(2) = vArr2

'change one of the elements as a test
vMainArr(2)(2) = 100

' 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

worked for me.
 
G

George Nicholson

Or, maybe add the 2 sub arrays to a collection object (which I'm fairly sure
does hold a pointer to vArr2) rather than another array (which just creates
a disconnected copy of the array at that point in time).

HTH,
 
J

Joe Dunfee

Thank you George and Tom;

I think I will go with the collection object, which I have just finished
reading up about. It seems to act very much like my masterarray, since
elements are referenced by an index number.

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