Enumerating object properties

derekhos
09-04-2003, 11:14 AM
Does any know how to enumerate thru the properties of an user defined class object? I have an object that has many properties, when assigning values to these properties I would like to have a generalized method where I can loop thru the properties to assign these values.

Van^
09-04-2003, 11:33 AM
Does any know how to enumerate thru the properties of an user defined class object? I have an object that has many properties, when assigning values to these properties I would like to have a generalized method where I can loop thru the properties to assign these values.

From what i understand, what you are asking is not possible. Properties are not sequential so that you can advance through them using an index. Rather, they are specialized methods that are members of the object/class. That said, when the object is in memory, the property can be allocated anywhere in its memory space and is not necessarily in the same place each time. The only option you have is to make the properties an index based system and publish an enumeration for the indecies. For example, you could have the following:



Public Enum eMyClassProperty

iPropertyOne = 0
iPropertyTwo
.
.
.
iPropertyX

End Enum

Public Property Get MyClassProperty(eID as eMyClassProperty) as Long

MyClassProperty = lngPropertyArray(eID)

End Property

Public Property Let MyClassProperty(eID as eMyClassProperty, lNewVal as Long)

lngPropertyArray(eID) = lNewVal

End Property



I can't remember the exact syntax for the Let, but it is similar. If you wanted separate property names for each property, you can't do it.

--Van^

00100b
09-04-2003, 12:08 PM
It is possible to enumerate the names if the Class is contained within an ActiveX DLL (or exposed by a Type Library by another library file type).

If you add a reference to "TypeLib Information" (if not present in references list, then browse to System or System32 directory and select the TLBINF32.DLL).

The following code will iterate through the members of a named item in the type library and identify whether or not it is a Property Put (Let/Set).

You will then need to use CallByName to invoke on an instantiated instance.

Private Sub Command1_Click()

Dim oTLI As TLI.TypeLibInfo
Dim oMbr As TLI.MemberInfo
Dim nIndex As Integer

Set oTLI = TLIApplication.TypeLibInfoFromFile("C:\Foo.DLL")
For nIndex = 1 To oTLI.TypeInfos.Count
If oTLI.TypeInfos.Item(nIndex) = "CFoo" Then
For Each oMbr In oTLI.TypeInfos.Item(1).Members
If oMbr.DescKind = DESCKIND_FUNCDESC And oMbr.InvokeKind = INVOKE_PROPERTYPUT Then
' This is your Property Let/Set
' Have fun with CallByName
'
' You didn't expect me to do it all for you did you?
End If
Next
Exit For
End If
Next nIndex

Set oMbr = Nothing
Set oTLI = Nothing

End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum