View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
deltaquattro deltaquattro is offline
external usenet poster
 
Posts: 65
Default How to write Property Let for the single elements of an array

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