PDA

View Full Version : Indexing Class Module Properties



dcherk1
10-30-2007, 09:50 PM
Hi,
I have created a class module containing a report with 20 fields of different variable types. Is there a way to index these properties, so that instead of invoking each of them by name, i could refer to them using a number (something like Property(1), property(2), etc.).

I asked this question a week ago or so about type variables and I was told that it was impossible with custom types. Maybe there is a way with class modules?
Anyway, sorry for the redundancy.

Bob Phillips
10-31-2007, 01:07 AM
Use a property that is an array type, then you can index it as you woulkd an array ina standard module.

dcherk1
10-31-2007, 11:01 AM
Can the properties be of different types themselves?

Bob Phillips
10-31-2007, 11:52 AM
No, as they will be part of an array.

Bob Phillips
10-31-2007, 12:01 PM
Let me re-phrase that ... yes!

Consider this class



Option Explicit

Private mmArray

Public Property Get MyValue(ByVal idx As Long)
MyValue = mmArray(idx)
End Property

Public Property Let MyValue(ByVal idx As Long, ByRef val)
If idx > UBound(mmArray) Then
ReDim Preserve mmArray(LBound(mmArray) To idx)
End If
mmArray(idx) = val
End Property

Private Sub Class_Initialize()
ReDim mmArray(0 To 0)
End Sub


and test it with this



Sub TestClase()
Dim myClass As Class1

Set myClass = New Class1
myClass.MyValue(3) = "Bob"
myClass.MyValue(9) = 123000
MsgBox myClass.MyValue(3) & " is worth " & myClass.MyValue(9) & " a year"
Set myClass = Nothing
End Sub