
Objects with arrays as public properties
Tony:
I'm not sure what a 'conventional' array is (a local one?), but there are
several ways to reference data in a class module. It is possible to make your
array a variant and thereby reference it as a public 'property' of the class -
this is frowned on generally because it exposes data directly to the owner app,
but if your 'property get' just does a simple assignment, what's the dif?
Check this out:
' SomeClass
Option Explicit
Public SomeArray As Variant
Private SngArray(1 To 100) As Single
Public Property Get EntireArray() As Variant
EntireArray = SomeArray
End Property
Public Property Get OneMember(Index As Integer) As Single
OneMember = SomeArray(Index)
End Property
Public Property Get UpperBoundOfArray() As Integer
UpperBoundOfArray = UBound(SngArray)
End Property
Private Sub Class_Initialize()
Dim i As Integer
ReDim SomeArray(1 To 100)
For i = 1 To 100
SngArray(i) = i * 1.1
Next i
SomeArray = SngArray
End Sub
If I instantiate SomeClass, I can reference any element of this array with
SomeClass.SomeArray(x), SomeClass.EntireArray(x), or SomeClass.OneMember(x).
I can also retrieve ubound(SomeClass.SomeArray), ubound(SomeClass.EntireArray)
and, of course, UpperBoundOfArray. For retrieving a single element or for
walking through the array, OneMember must be fastest, but if you need to pass
the array to some other function, EntireArray does the trick. (I'm really
uncomfortable using SomeArray for the above reasons, but I've also seen some
evidence that going through the Property Get will be faster than the direct
access anyway!)
Does this help?
Quote:
> Hi,
> I've created a fairly complex object model and would like to define an
> array
> example: Public property dummyarray(1 to 100) as single
> as a public property for the object, but if I've understood correctly VB 5
> does not support that kind of object interfaces.
> I've tried two different workarounds
> 1. Define a local property and a property Get function that returns a
> specific element of the array
> example:
> Private property localdummyarray(1 to 100) as single
> Property Get dummyarray(index) as single
> dummyarray = localdummyarray(index)
> end property
> 2. Define a class eg. (arrayclass) which contains the property, property
> Get function described in 1. and then define an object type property in the
> real class.
> Example:
> Class: ArrayClass
> Private property localdummyarray(1 to 100) as single
> Property Get dummyarray(index) as single
> dummyarray = localdummyarray(index)
> end property
> ...
> Class: RealClass
> Public property realarray as new ArrayClass
> Both of these workarounds work but are about 10 times slower to use than
> conventional arrays. Since speed is crucial in my project I've would be
> really grateful if someone would know of a better workaround.
> Tony