Re: How do I Create 'ragged' arrays in Excel VBA?
- From: "Tom Ogilvy" <twogilvy@xxxxxxx>
- Date: Sun, 11 Mar 2007 17:07:14 -0400
I disagree with John's description of references in this instance.
When you run thestatement 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" <dempey@xxxxxxxxxx> wrote in message
news:Xns98EFDF25967FDdempeycruziocom@xxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: How do I Create 'ragged' arrays in Excel VBA?
- From: David Empey
- Re: How do I Create 'ragged' arrays in Excel VBA?
- From: John Coleman
- Re: How do I Create 'ragged' arrays in Excel VBA?
- References:
- How do I Create 'ragged' arrays in Excel VBA?
- From: David Empey
- How do I Create 'ragged' arrays in Excel VBA?
- Prev by Date: Re: Help with code
- Next by Date: Re: How do I Create 'ragged' arrays in Excel VBA?
- Previous by thread: Re: How do I Create 'ragged' arrays in Excel VBA?
- Next by thread: Re: How do I Create 'ragged' arrays in Excel VBA?
- Index(es):
Relevant Pages
|