Setting dimensions for Dynamic Arrays

D

devlei

I use dynamic arrays to hold data from certain Calendar items based on
specified criteria .

Before extracting the data I don't know how many items will meet the
criteria so I do one iteration through all the items to count, then
Redim the array, then iterate a second time to fill the array. But
with 900 items in the folder using the OOM, this takes too long.

Is there an easier way to Redim the array without iterating through
twice?

With thanks
 
K

Ken Slovak - [MVP - Outlook]

Set a Restriction on the Items collection of the folder that filters on your
valid data condition and that will return a filtered Items collection. Use
the Count property to see how many items you need to set up for your array.
 
M

Michael Bauer

Hi,

in this case I´d use an approximately value for the dimension. The
sample I´ve just hacked without testing, please check with a little
value for ARRAY_BUFFER, e.g. 1 and 2.


Private Const ARRAY_BUFFER As Long = 1000
Private m_lPos As Long
Private m_lCnt As Long
Private m_aData() As Variant

Private Sub InitArray()
m_lPos = -1
m_lCnt = ARRAY_BUFFER
ReDim m_aData(ARRAY_BUFFER - 1)
End Sub

Private Sub AddElement(v As Variant)
m_lPos = m_lPos + 1
m_aData(m_lPos) = v
If m_lPos = (m_lCnt - 1) Then
m_lCnt = m_lCnt + ARRAY_BUFFER
ReDim Preserve m_aData(m_lCnt - 1)
End If
End Sub

Private Sub TrimArray()
Select Case m_lPos
Case Is < 0
m_lCnt = 1
ReDim m_aData(0)
Case Is < (m_lCnt - 1)
m_lCnt = m_lPos + 1
ReDim Preserve m_aData(m_lPos)
End Select
End Sub

Public Sub TestArray()
InitArray
AddElement "1"
AddElement "2"
AddElement "3"
TrimArray
End Sub
 
M

Michael Bauer

Oh no, that´s to easy :)

--
Viele Grüße
Michael Bauer - MVP Outlook


Ken Slovak - said:
Set a Restriction on the Items collection of the folder that filters on your
valid data condition and that will return a filtered Items collection. Use
the Count property to see how many items you need to set up for your
array.
 
M

Michael Bauer

I wish it would be! Currently I´ve to make my first steps with mambo
(CMS) and feel like some people here: Nothing works and I don´t know
where to start...

--
Viele Grüße
Michael Bauer - MVP Outlook


Ken Slovak - said:
Easy is good sometimes, no? :)
 

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