Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Property Let to define an array variable in a class module 1

Status
Not open for further replies.

InyoTom

Technical User
Feb 4, 2000
33
0
0
US
I want to make an array variable be a property definition in a class module. In the class module, statements such as:

Dim dblArray() as Double

Property Let MyProperty(dblWPT As Double)
dblArray() = dblWPT
End Property

Access Help does not talk about arrays as properties, only scalar varaibles as properties.

1) Is this possible?
2) How to? I have tried all sorts of syntax variations, and received all sorts of error messages.


Tom Budlong
TomBudlong@Adelphia.net

 
You can't actually get the property type to be an array. However, you can make the property type a variant and then test whether what's being passed is an array of Doubles. I got the following class MyClass to work:
Code:
Dim adblValues() As Double

Public Property Get Values() As Variant
    Values = adblValues
End Property
Public Property Let Values(ArrayOfDbl As Variant)
    If VarType(ArrayOfDbl) = vbArray + vbDouble Then
        adblValues = ArrayOfDbl
    Else
        Err.Raise 5, "MyClass", "Values property requires an array of Double values"
    End If
End Property
I tested it with this code:
Code:
Public Sub Test()
    Dim tmp As New MyClass
    Dim vals(0 To 3) As Double
    
    vals(1) = 1#
    vals(2) = 2#
    vals(3) = 3#
    tmp.Values = vals
    Debug.Print tmp.Values(1)
    Debug.Print tmp.Values(2)
    Debug.Print tmp.Values(3)
End Sub

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Works like a charm. Not in a hundred years would I have figured it out on my own.

Thanks, Rick. Tell your boss I approve your raise.

... Tom Budlong

Tom Budlong
TomBudlong@Adelphia.net

 
Not that I necessarily LIKE it, but VB (at least VB6)supports arrays in class modules. Partially because of the 'likeness' and mostly due to the lack of actual need, I haven't use this is a while, but I have done so, w/o any trouble. Sinve Ms. A. currently (ver 2K and later) uses (core) VB as the VBA engine, I can't imagine that this is not included.

MichaelRed


 
Actually, I am working in the CorelDraw flavor of VB, which claims to be version 6.3. (Usually I am working in good old MS Access.) I can't imagine MS would remove array support for class modules when selling VB to Corel.

Perhaps my problem is syntax, but VB's explanations when it complains are so obscure it's hard to tell what's wrong.

Anyway, Rick Sprague's 'invention' works well, and I am using it.

If you have sample code that works, I would love to see it.

Tom Budlong

Tom Budlong
TomBudlong@Adelphia.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top