View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_4_] Bob Phillips[_4_] is offline
external usenet poster
 
Posts: 834
Default How to write Property Let for the single elements of an array

I haven't looked too hard at your code, but here is a thought.

When you initialise the class setup the array big, say 1000 elements. Use a
class variable to control the next index and just use this to allocate to
the next element. This way, the array will be 1,000 elements in size
internally to the class, but to any code using an instance of that class it
will look much smaller. When your internal index gets to 1000, bump it up
(Redim) by some order of magnitude. This way, you only redim once in a blue
moon.

--

HTH

Bob

"deltaquattro" wrote in message
...
Hi all,

thanks to Bob's and other people's suggestions, I'm now able to assign
to all elements of an array property in a class module. But what if I
need to assign to a *single* element? It looks like I have to Redim it
each time...
Example:

' Class Module
Option Explicit
Option Base 1

Private pYValues() As Double

' not sure if this Class_Initialize Sub makes sense: IMO it would
' in other languages, but maybe in VBA is redundant
Private Sub Class_Initialize()
ReDim pYValues(1) As Double
End Sub

Public Property Get YValues() As Double()
YValues = pYValues
End Property
Public Property Let YValues(Values() As Double)
pYValues = Values
End Property
Public Property Get YValue(Index As Long) As Double
YValues = pYValues(Index)
End Property
Public Property Let YValue(Index As Long, Value As Double)
' problem!!!
pYValues(Index) = Values
End Property

The problem is in Property Let YValue: if I add en element to the
array, I get a "subscript out of range" error. So I guess I should
write it like this:

Public Property Let YValue(Index As Long, Value As Double)
If UBound(pYValues) < Index Then Redim Preserve pYValues(Index)
pYValues(Index) = Values
End Property

but redimensioning the array each time I add a single element is
really slow! Maybe I should write a "Resize" method, something like:

Public Sub Resize(Nelements As Long)
Redim Preserve pYValues(Nelements)
End Sub

resize the array before adding each element, and then add. What do you
think? Any better ideas? Thanks in advance,

Best Regards

deltaquattro