Can I pass a property to a method?

I’d like to have a method with parameters called alteredproperty as string, myvalue as boolean (for example)

This would then be called like :-

mymethodwithparameter(“Visible”,False)

and then the method would do something like (which doesn’t work as I’ve put it here)

someobjectonwindow.alteredproperty.stringvalue=myvalue

Obviously, the above is more like pseudocode but hopefully someone will understand what I mean/want!

Thanks.

If you aren’t concerned about performance you can do this via Introspection.

I’ve just hacked the following together that should do what you want:

[code]Sub SetPropertyValue(pObject As Object, pPropertyName As String, pValue As Variant)
Dim found As Boolean
Dim typeInfoObj As Introspection.TypeInfo
Dim propertyInfoObjList(-1) As Introspection.PropertyInfo
Dim count, i As Int32
Dim thePropertyInfoObj As Introspection.PropertyInfo

found = False

typeInfoObj = Introspection.GetType(pObject)

propertyInfoObjList = typeInfoObj.GetProperties

count = UBound(propertyInfoObjList)
For i = 0 To count
thePropertyInfoObj = propertyInfoObjList(i)

If thePropertyInfoObj.Name = pPropertyName Then
  thePropertyInfoObj.Value(pObject) = pValue
  
  found = True
  
  Exit For
End If

Next

If found = False Then
Raise New KeyNotFoundException
End If
End Sub[/code]

Example:
Set the Title property for a window:

SetPropertyValue(Self, "Title", "Hello")

Edit…

To make this a bit more friendlier, put the method into a module and change the declaration:

Extends pObject As Object, pPropertyName As String, Assigns pValue As Variant

Examples of setting properties on a window:

Self.SetPropertyValue("Title") = "Hello" Self.SetPropertyValue("Width") = 100

I have a feature request out there for something like GetTypeInfo( object.Property ) As PropertyInfo.

[quote=463224:@Michel Bujardet]Create a method with parameters alteredproperty as string, myvalue as boolean, that’s it.

Does that answer your question ?[/quote]

No because that’s just the start of my explanation of what I want. I want to pass those method parameters (which yes, I do know how to use) and use them with object properties as per my question.

Wow Kevin, thank you for your detailed post and the time to write that code.

Is performance in a desktop app doing this likely to pose a real world problem then?

[quote=463216:@Kevin Gale]If you aren’t concerned about performance you can do this via Introspection.

I’ve just hacked the following together that should do what you want:

[code]Sub SetPropertyValue(pObject As Object, pPropertyName As String, pValue As Variant)
Dim found As Boolean
Dim typeInfoObj As Introspection.TypeInfo
Dim propertyInfoObjList(-1) As Introspection.PropertyInfo
Dim count, i As Int32
Dim thePropertyInfoObj As Introspection.PropertyInfo

found = False

typeInfoObj = Introspection.GetType(pObject)

propertyInfoObjList = typeInfoObj.GetProperties

count = UBound(propertyInfoObjList)
For i = 0 To count
thePropertyInfoObj = propertyInfoObjList(i)

If thePropertyInfoObj.Name = pPropertyName Then
  thePropertyInfoObj.Value(pObject) = pValue
  
  found = True
  
  Exit For
End If

Next

If found = False Then
Raise New KeyNotFoundException
End If
End Sub[/code]

Example:
Set the Title property for a window:

SetPropertyValue(Self, "Title", "Hello")

Edit…

To make this a bit more friendlier, put the method into a module and change the declaration:

Extends pObject As Object, pPropertyName As String, Assigns pValue As Variant

Examples of setting properties on a window:

Self.SetPropertyValue("Title") = "Hello" Self.SetPropertyValue("Width") = 100[/quote]

[quote=463237:@Rod Pascoe]Wow Kevin, thank you for your detailed post and the time to write that code.

Is performance in a desktop app doing this likely to pose a real world problem then?[/quote]
Introspection is definitely considered “expensive” and “slow” but honestly in most cases that expense is negligible. Unless you’re doing a heavy computing math application or image processing, I doubt the cost will be impactful. I use a ton of introspection to serialize and deserialize objects, and it works great. Think of it as a scale where bitwise operations are extremely fast, operations on primitives are fast, dealing with objects and method calling is slow, and introspection/reflection is even slower. But you can still do 1000’s of introspection calls within a second without a user noticing any problem.

[quote=463237:@Rod Pascoe]Wow Kevin, thank you for your detailed post and the time to write that code.

Is performance in a desktop app doing this likely to pose a real world problem then?[/quote]
I haven’t benchmarked the code but I probably wouldn’t want to call it lots of times within a loop.

Thanks Brock.

Thanks Kevin,

I’ll only be using it a handful of times for what I have in mind and those should be several minutes apart so all good.

Nice to know though, always good to learn.