Objects with arrays as public properties 
Author Message
 Objects with arrays as public properties

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



Sun, 20 May 2001 03:00:00 GMT  
 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



Sat, 26 May 2001 03:00:00 GMT  
 Objects with arrays as public properties
Oops - The line
Redim SomeArray(1 to 100)
in the class_initialize is, of course, useless/senseless/meaningless . . .


Sat, 26 May 2001 03:00:00 GMT  
 Objects with arrays as public properties


Quote:
>Oops - The line
>Redim SomeArray(1 to 100)
>in the class_initialize is, of course, useless/senseless/meaningless . . .

Worse than that. I should give you an error because your original Dim
SomeArray(1 To 100) made it a static array that can't be resized.

--
Myrna Larson



Sat, 26 May 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Private and Public Properties of type Array

2. Object Properties, Array Properties

3. Public array in an object module

4. Only public datatypes defined in public object modules...

5. Public function with public-object parameter

6. Host for Property Pages or Public Property Page

7. Host for Property Pages or Public Property Page

8. Array as a Property of an Object, and Collection

9. Using an array as an object property

10. Property Array in an Object ?

11. array object property possible?

12. REQ : How to create an activex (VB5) with property like mycontrol.array(x).properties

 

 
Powered by phpBB® Forum Software