ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Get Property value of class instance by passing string property name (https://www.excelbanter.com/excel-programming/399625-get-property-value-class-instance-passing-string-property-name.html)

[email protected]

Get Property value of class instance by passing string property name
 
Does anyone know how to get the value of a property of an instance of
a class by passing the string as the name of the property?

I have seen it somewhere before e.g.
GetPropertyValueForClass(myClass,"PropertyName")

Any help would be really appreciated

Thanks

Mark


Peter T

Get Property value of class instance by passing string property name
 
Hi Mark,

Are you thinking of CallByName, eg

' code in normal module
Sub test()
Dim cls As Class1
Set cls = New Class1
CallByName cls, "PropA", VbLet, 123
MsgBox CallByName(cls, "PropA", VbGet)
End Sub

' code in Class1
Private mnID As Long

Public Property Let PropA(ID As Long)
mnID = ID
End Property

Public Property Get PropA() As Long
PropA = mnID
End Property


Regards,
Peter T


wrote in message
oups.com...
Does anyone know how to get the value of a property of an instance of
a class by passing the string as the name of the property?

I have seen it somewhere before e.g.
GetPropertyValueForClass(myClass,"PropertyName")

Any help would be really appreciated

Thanks

Mark




Chip Pearson

Get Property value of class instance by passing string property name
 
Use CallByName:

Dim Res As Variant
Dim PropName As String
Dim Obj As New Class1
' some code to set up Obj
PropName = "MyProperty"
Res = CallByName(Obj, PropName, VbGet)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

wrote in message
oups.com...
Does anyone know how to get the value of a property of an instance of
a class by passing the string as the name of the property?

I have seen it somewhere before e.g.
GetPropertyValueForClass(myClass,"PropertyName")

Any help would be really appreciated

Thanks

Mark




All times are GMT +1. The time now is 04:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com