You are correct. An even simpler example to show that Array
assignments don't create an alias would be something like
Dim A(), B() as long
Redim A(1 to 10)
Redim B(1 to 10)
A(1) = B
B(1) = 5
msgbox A(1)(1) 'won't return 5
my speculation was based on trying to figure out why VBA seems to
require B here (perhaps hidden in a function call as in my second
post) instead of a simple
Dim A(1 to 10) as variant
For i = 1 to 10
ReDim A(i)(1 to i) 'ReDim (A(i))(1 to i) isn't any better
Next i
(which is a syntax error).
If A(i) is a Variant and ReDim is a valid statement for Variants, then
why not for A(i)? My first guess was that B was playing some essential
role as the target of a reference, but it looks more like it is just
plugging a gap in VBA's syntax (although if you want the sub-arrays to
be of type other than variant something like B or a function call
would be required).
Thanks for the correction
-John Coleman
On Mar 11, 5:07 pm, "Tom Ogilvy" <twogi...@msn.com> wrote:
> I disagree with John's description of references in this instance.>When you run the
>
> statement A(i) = B you are establishing a reference to the current
> array B - a reference which won't be removed until *A(i)* is
> reassigned (or destroyed).
>
> I don't believe this is correct. This should convince you that A(i) is
> different from B. In otherwords, the array B is copied to A(i), not
> referenced: (since B has not been reassigned, if it were only referenced,
> A(i)(j) = B(j). But it doesn't. )
>
> Sub RaggedArray()
>
> Dim A() As Variant, B() As Long, i As Long, j As Long
> Dim l As Long
> ReDim A(1 To 10)
> For i = 1 To 10
> ReDim B(1 To i)
> For j = 1 To i
> B(j) = Int(Rnd() * 100 + 1)
> Next
> A(i) = B
> For j = 1 To i
> A(i)(j) = 10 * i + j
> Next j
> If i = 5 Then
> For j = 1 To i
> Debug.Print i, j, A(i)(j), B(j)
> Next
> End If
>
> Next i
>
> End Sub
>
> So you should have no concerns using this approach.
>
> --
> Regards,
> Tom Ogilvy
>
> "David Empey" <dem...@cruzio.com> wrote in message
>
> news:Xns98EFDF25967FDdempeycruziocom@64.209.0.87...
>
>
>
> > The following code seems to work, but is it safe?
>
> > Sub RaggedArray
>
> > Dim A() as Variant, B() as Long, i as Long, j as Long
>
> > ReDim A(1 to 10)
> > For i = 1 to 10
> > ReDim B(1 to I)
> > A(i) = B
> > For j = 1 to i
> > A(i)(j) = 10 * i + j
> > Next j
> > Next i
>
> > End Sub
>
> > This seems to create an array A whose elements are arrays
> > of varying lengths, which is what I want, but can I be
> > sure the elements of A won't be overwritten by some other
> > piece of code that needs to use memory? Does Visual
> > Basic know the elements of A exist?
>
> > Am I even asking a sensible question?
>
> > --
> > Dave Empey
>
> > Remember, if you're doing any major experiments in stellar
> > dynamics, always mount a scratch star first! --Richard Todd- Hide quoted text -
>
> - Show quoted text -
|