Addressing object properties

Is there a mechanism for addressing object properties (or constants) in a manner that is similar to the techniques used for identifying folder items?

So, for example, is it possible to do something like:

Object.Property(“theName”) = value

The obvious first question is “why not just use Object.theName?” It’s because I have a large number of properties to set and I would like to create a generic routine to do so.

Consider 100 sets of the following information.

Object.Property1 DataName1, DataValue1

Property1 and DataName1 have identical values. The object is to get each DataValue into its comparable Property. If each Property could be addressed by using the (identical) DataName, values could be quickly assigned.

Can this be done? Or is there a better approach?

Thanks,
Will

Not directly. However, you could use a dictionary instead of properties. You could also use operator_lookup to use the same syntax as properties. One problem with using a dictionary though, is that you lose any compile time validation.

See the attached example project.

Xojo Class Operator Lookup Test.xojo_binary_project.zip (4.3 KB)

You could use Introspection to loop through all the properties and assign the value when you find the one you’re looking for:

Dim props() As Introspection.PropertyInfo
props = Introspection.GetType(TheObject).GetProperties()
For i As Integer = 0 To props.LastIndex
  If props(i).Name = "theName" Then
    props(i).Value(TheObject) = value
  End If
Next i

@William_Phillips - Introspection can probably meet your need, however…

Using introspection adds real complexity and it is generally a “heavier” task. (uses a lot of extra cycles vs. just setting a property)

Here are some relevant sections from the Xojo docs:

https://documentation.xojo.com/api/language/introspection.html
https://documentation.xojo.com/api/language/propertyinfo.html

I knew you guys would have some good thoughts.

I like both the dictionary and introspection ideas. I think I will give introspection a go since the app is lightweight and has cycles to spare. I have been meaning to play around with introspection anyway.

If that approach turns out to be too burdensome I’ll fall back to the dictionary approach.

Thanks for the guidance.