tips for initializing/reusing dynamic arrays

E

Eric

I have the code below for doing this now, as part of a function. It needs to
take into account that:
- the array may not be dimensioned yet
- the array may be dimensioned, but Item(0) is 'empty"
- the array may be added to (this function may be called multiple times and
add to the array on each call)

It works and all, but looks so...inelegant. Anyone see anything I'm doing
wrong here?

I'm wondering if I could encapsulate this generically somehow. It seems the
only tricky part in doing that would be the test for Item(0) being empty,
which seems like it needs to be a logical test unique to the items you are
storing. Any ideas? Worth it?

Thanks, Eric
---------------------------
Dim restoreIndex As Integer
On Error Resume Next ' in case the array is not dimensioned
yet
Dim bFirstAdd As Boolean ' true if the array is either not
dimension or dimensioned but empty
' tests for emptiness, and will generate error 9 if not dimensioned
bFirstAdd = IsNothing(m_filterRestore(0).m_wksName)
If Err.Number = 9 Then
' not dimensioned yet. so let's do it
ReDim m_filterRestore(0)
bFirstAdd = True
On Error GoTo 0
End If
restoreIndex = IIf(bFirstAdd, 0, UBound(m_filterRestore) + 1)
ReDim Preserve m_filterRestore(restoreIndex + (fltrs.Count))
 

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