LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 65
Default How to write Property Let for the single elements of an array

Hi Bob!

Thanks very much for your suggestion, brilliant as always. I
understand that my concern may be exaggerated for a modern PC, but in
my class I have also some matrices (2D arrays), and in that case maybe
the memory problem could be more relevant. Anyway, your code enables
me to handle both cases very easily: clearly, for matrices I have to
be sure that I Redim Preserve only the last dimension, but other than
that, the code is mainly the same and that's very useful for me.
Thanks again,

Best Regards

deltaquattro


On 16 Mar, 15:52, "Bob Phillips" wrote:
A bit more than that, you need to keep an index (a pseudo-upper bound for
the array) as well as the array size, an increment, and load it all

'------ Class Module
Option Explicit
Option Base 1

Private pYValues() As Double
Private ArrayIndex As Long
Private ArraySize As Long
Private ArrayInc As Long

Private Sub Class_Initialize()
* * ArraySize = 10
* * ArrayInc = 5
* * ReDim pYValues(1 To ArraySize) 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
* * YValue = pYValues(Index)
End Property
Public Property Let YValue(Index As Long, Value As Double)
* * If Index ArrayIndex Then

* * * * ArrayIndex = ArrayIndex + 1
* * * * If ArrayIndex ArraySize Then

* * * * * * ArraySize = ArraySize + ArrayInc
* * * * * * ReDim Preserve pYValues(1 To ArraySize)
* * * * End If
* * End If
* * pYValues(Index) = Value
End Property

and use like so

Sub testclass()
Dim cls As Class1
Dim i As Long

* * Set cls = New Class1

* * For i = 1 To 12

* * * * cls.YValue(i) = i
* * Next i

* * For i = 1 To 12

* * * * Debug.Print cls.YValue(i)
* * Next i

* * cls.YValue(11) = 22

* * For i = 1 To 12

* * * * Debug.Print cls.YValue(i)
* * Next i
End Sub

Quite honestly, 1000 doubles in a modern machine is not going to affect
anything.

--

HTH

Bob

"deltaquattro" wrote in message

...

Hi Bob,


so you're suggesting something like this? I omitted all the properties/
methods not directly related to the original question, so as to keep
the posted code shorter:


' Class Module
Option Explicit
Option Base 1


Private pYValues() As Double
Private ArraySize As Long


Private Sub Class_Initialize()
* *ArraySize = 1000
* *ReDim pYValues(ArraySize) As Double
End Sub


Public Property Let YValue(Index As Long, Value As Double)
* *If Index ArraySize Then
* * * *ArraySize = Index
* * * *ReDim pYValues(ArraySize)
* *End If
* *pYValues(Index) = Value
End Property


I'm a bit unsatisfied with the waste of memory: guess it can't be
helped, though. Thank you very much for your suggestion,


Best Regards


deltaquattro


On 16 Mar, 14:23, "Bob Phillips" wrote:
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




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
elements in an array Chip Pearson Excel Programming 2 November 23rd 07 12:19 PM
elements in an array Alan Beban[_2_] Excel Programming 0 November 21st 07 08:21 PM
array elements count madeleine[_2_] Excel Programming 6 December 8th 06 03:36 AM
Reference Elements w/in an Array M Moore Excel Discussion (Misc queries) 2 October 16th 06 03:33 PM
Shifting Array Elements Trip[_3_] Excel Programming 6 July 30th 05 07:30 PM


All times are GMT +1. The time now is 01:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"