PC Review


Reply
Thread Tools Rate Thread

Changing values of an array of arrays

 
 
=?Utf-8?B?Sm9lIER1bmZlZQ==?=
Guest
Posts: n/a
 
      11th Jun 2007
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      11th Jun 2007
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.

--
Regards,
Tom Ogilvy


"Joe Dunfee" wrote:

> 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

 
Reply With Quote
 
George Nicholson
Guest
Posts: n/a
 
      11th Jun 2007
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,

"Tom Ogilvy" <(E-Mail Removed)> wrote in message
news:14272678-90AB-43AC-829C-(E-Mail Removed)...
> 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.
>
> --
> Regards,
> Tom Ogilvy
>
>
> "Joe Dunfee" wrote:
>
>> 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



 
Reply With Quote
 
=?Utf-8?B?Sm9lIER1bmZlZQ==?=
Guest
Posts: n/a
 
      13th Jun 2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
RENEWED-- Arrays: Counting multiple values within array Trilux_nogo Microsoft Excel Worksheet Functions 5 20th Apr 2007 01:30 AM
Jagged Arrays Problem - How to Assign Arrays to an Array Zigs Microsoft Excel Programming 3 11th Apr 2007 01:39 AM
Changing values in an array using for each =?Utf-8?B?QW5kcmV3IEhhbGwgTlo=?= Microsoft Excel Programming 2 19th Mar 2007 12:50 AM
Using known arrays to calculate an array of new values Brett Microsoft Excel Programming 8 19th Sep 2006 06:32 AM
Arrays - declaration, adding values to arrays and calculation Maxi Microsoft Excel Programming 1 17th Aug 2006 04:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:54 AM.