Solved

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

Posted on 2014-04-14
8
1,022 Views
Last Modified: 2014-04-28
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

0
Comment
Question by:Rob Rudloff
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39999664
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
0
 

Author Comment

by:Rob Rudloff
ID: 39999851
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.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39999861
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 250 total points
ID: 39999977
VB6 doesn't have a method that will allow you to loop thru all the properties of a class/form.

But you can have parameters on Get/Let/Set properties.  So it's possible to add an Index parameter to your properties and look thru the property with the index.  However, all variations of the property (Get/Let/Set) must have the same parameter.  You can't have an index on a Get property without the Let (or Set) property of the same name having the same parameter.

The syntax for the declarations would look something like this:
Public Property Get MyProp( Index as Integer ) as String
Public Property Let MyProp( Index as Integer, Value as String)

Open in new window

0
 

Author Comment

by:Rob Rudloff
ID: 40000089
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"
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 100 total points
ID: 40000240
You could create a public Propeties collection/dictionary function that would return name/value pairs of properties.

Take a look at my class item filtering article for an example of returning multiple instances of class objects.  In your case, the objects would have a name property and a value property.
http:A_8450-Class-Item-Filtering-Via-Dynamic-Scripting.html
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 250 total points
ID: 40001663
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"

Correct... though 'technically' the 'Index' would be a simple parameter.  Your logic behind the property 'Let MyProp( Index as integer, Value as String)' would interpret the parameter as an array index (if the member variables you are trying to access are in an array).

So I would expect your class logic to look like one of the following:
Private m_FirstName as String
Private m_LastName as String
Private m_Position as String
Public Property Let MyProp(Index as Integer, Value as String)
  If Index = 1 Then
    m_FirstName = Value
  ElseIf Index = 2 Then
    m_LastName = Value
  ElseIf Index = 3 Then
    m_Position = Value
  Else
    ASSERT( FALSE ) 'Break in debug mode so I know I've passed an invalid Index
    Err.Raise( 5 )
  End If
End Property

Open in new window

or
Private m_MyProp(1 to 3) as String
Public Property Let MyProp(Index as Integer, Value as String)
  m_MyProp(Index) = Value
End Property

Open in new window


And then the corresponding Property Get coded in a similar manner.
0
 

Author Closing Comment

by:Rob Rudloff
ID: 40028122
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question