Solved

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

Posted on 2014-04-14
8
852 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 45

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 45

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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now