Link to home
Start Free TrialLog in
Avatar of Rob Rudloff
Rob RudloffFlag for United States of America

asked on

VB6 - How To Loop Through All Properties In A Class?

Hi.  Using old-fashioned VB6, is it possible to loop through all the properties in a class I have created?

For example let's say I have a Class called clsTest that has 3 properties (strings) called Prop1, Prop2, and Prop3.   In clsTest, I have a Let and Get for each of them (like in the code at the bottom here).  I'd like to simply loop through the properties somehow, and display the value.

Assuming I Dim a class "MyClass" and load it with values --  I'd like to print the values, something like this (very wrong) code:

For x = 1 to 3  'number of properties in the Class
debug.print MyClass.property(x).value
next x

Thanks for your suggestions.


Private msProp1 As String
Private msProp2 As String
Private msProp3 As String

Public Property Let Prop1(ByVal vData As String)
    msProp1 = vData
End Property

Public Property Get Prop1() As String
     Prop1 = msProp1 
End Property

Public Property Let Prop2(ByVal vData As String)
    msProp2 = vData
End Property

Public Property Get Prop2() As String
     Prop2 = msProp2 
End Property

Public Property Let Prop3(ByVal vData As String)
    msProp3 = vData
End Property

Public Property Get Prop3() As String
     Prop3 = msProp3 
End Property

Open in new window

Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Well I've never done this and it's not tested but how about changing

Private msProp1 As String
Private msProp2 As String
Private msProp3 As String

to

Private msProps(2) As String

Then if VB6 lets you to do this, etc you can just loop through msProps.

Public Property Let Prop1(ByVal vData As String)
    msProp(0) = vData
End Property
Avatar of Rob Rudloff

ASKER

I will try something like that, but the issue there may be that the msProp vars are "private", and only used when you call the Public "Get" or the "Let" functions.  They won't be visible outside the Class code itself.  
So, if I have dim'd a myClass in some other form in the app, I won't have access to those msProp values ... I think.  

Perhaps I can make them "public" too, but that seems not-normal.
You can keep msProps as Private and add a method to the class like this

Public Sub DisplayValues()

Dim intIndex As Integer

For intIndex = 0 to UBound(msProps)
    Debug.Print msProps(intIndex)
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HooKooDooKu
HooKooDooKu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, using HooKooDooKu's method ---
would calling "Lets" mean using the same property name "MyProp", but with a different Index?  As in:

    myClass.MyProp(1) = "Bill"
    myClass.MyProp(2) = "Gates"
    myClass.MyProp(3) = "CEO"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
aikimark's answer (in the link) seems the the "proper" hard-core way to do it, but in my case I was able to get away with the simpler example from HooKooDooKu

Thanks all.