Re: How do I Create 'ragged' arrays in Excel VBA?
- From: "John Coleman" <jcoleman@xxxxxxxxxxxxxx>
- Date: 11 Mar 2007 07:21:03 -0700
Interesting example. I think that what happens is the following: VBA's
garbage collection is based on reference counting. 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). When ReDim B(1 to i) is run a brand new
array is allocated for B. This usually results in the old array being
garbage collected - but here you still have a reference keeping it
alive. Your code seems to work but it is not clear to me that you are
guaranteed that a ReDim B always allocates a separate memory block for
the new array and never overwrites the old B (unless it is ripe for
garbage collection) . In other words - I'm not sure if you have found
a subtle use of a documented behavior or a convienent use of an
undocumented. I suspect the former, but am not 100% sure. Maybe
someone with more knowledge of VBA's memory management will chip in.
-John Coleman
On Mar 11, 1:56 am, David Empey <dem...@xxxxxxxxxx> wrote:
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: need help on how to grey out one option button in one group box based on the selection of another option button in another group box
- Next by Date: Copy paste from Excel to Outlook - Graphs, pivots & cells
- Previous by thread: 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
|