Access property value at runtime

Hello all. I’ve only been using Xojo for a year or so, so I’m hoping this is a simple answer I’m just overlooking.

I’m trying to give users indirect access to property values so they can use them to compute math problems. What I have is a table in a database that has the user’s assigned property name: “Name”, and the name of the property in the IDE: “Property”. I want to create a dictionary that holds the “Name” and the property value. The way I thought I could do this was with introspection, where I would take the “Property” name from the table, find the window’s property that matches “Property”, then take that value and place in the dictionary with the “Name” as the key. But I can’t seem to get the property’s value with introspection, is this correct?

This is what I’ve done so far:

  Dim w As Window = Window1
  Dim t As Introspection.TypeInfo = Introspection.GetType(w)
  Dim ip() As Introspection.PropertyInfo = t.GetProperties
  Dim e As New Dictionary
  
  App.live_db
  If App.db.Connect Then
    Dim rs As RecordSet
    Dim sql As String
    sql = "SELECT Name, Property FROM " + App.table_path + " "
    rs = App.db.SQLSelect(sql)
    
    If rs <> Nil Then
      While Not rs.EOF
        For i As Integer = ip.Ubound DownTo 0
          If ip(i).Name = rs.Field("Property").StringValue Then
            Dim p As Variant = ip(i)
            e.Value(rs.Field("Name").StringValue) = p
            Exit
          End
        Next
        rs.MoveNext
      Wend
      rs.Close
    End
    
    If App.db.ErrorCode > 0 Then
      MsgBox App.db.ErrorMessage
    End
    App.db.Close
  End

When adding a dictionary entry as e.Value(“Name”) = IDEproperty, the IDEproperty shows the value. Thanks for any help.

Edit:
I will add, if there was a way I could create a pairing of a String and Property (not it’s value), that would work more in my favor as the current properties I’m using will be continually updating and this will require recreating the dictionary with every change of property values.

You can get a property value this way:

dim value as variant = ip(i).Value(w)

When you get Introspection data, you are getting generic information for the class. Every Window1 will have the same set of properties, methods, etc. To get information out of the specific instance, you have to feed it that instance.

In this example code, if w is nil, your app will probably crash.

Understood. Thanks Kem!

Would this be a good place to use XojoScript?

I’m not very well educated on XojoScript. I’ll look into this.

@Tim Hare , correct me if I’m wrong, but XojoScript allows runtime strings to be interpreted as code from an end user? If so, yes this could be useful, I’ll have to think more on the idea. I know how I’m going to explain this is going to be very obscure and possibly confusing, because I feel going into full detail would be a novel.

I have a handful of parameters that mirror a manufacturing setup that are in a set of controls that an end user can set (machine speed, amount of material needed, etc.). Those values are then put into existing, hard-coded equations to calculate properties in the application. On one hand, I’d like a user to be able to modify the equations that calculate the properties (removing work from the programmer), which are a mix of static numbers and the values from the selected controls. As well as take the calculated properties, and create their own temporary equations for whatever reason.

I could see how XojoScript would work in both of these instances, but wouldn’t the end user need to know the exact name of the property in the IDE to be able to reference it as well as know to use popupmenu1.value for the control values? I’m reading further and trying to find examples to get a better grasp.

Either way, they need to know your “vocabulary” - the strings you use to associate with each value. With XojoScript, you create an interface layer (the context) that provides variables to the user. The context would map the values to variables that you have named (the Keys in your dictionary example) in ways that are meaningful to the end user. So, no, they don’t need to know about popupmenu1.value, they need to know about MachineSpeed, which you conveniently map to popupmenu1.value. The advantage of XojoScript is that you don’t have to interpret their formula and produce the result.

Okay. So from my brief experiments, this works pretty well for what I’m looking for so far. Thank you.

I want to continue experimenting and learning, but what I have been able to determine so far is that XojoScript can call a local method from the user’s input, wherein the method can then return the value of any property (local or global). On open I place all the desired method’s names into an array. I then created a custom textfield with an autocomplete referencing that array. So now a user can type a formula into this textfield, and an auto-completed method name will appear when making reference to certain properties (via those methods). Everything is calculating correctly.

Now if I want to take this one step further, and allow the user to create their own terminology for those property/methods, is it possible? I’d go the route of having the existing setup, where a database stores the actual method name and the user’s method name for that method. But when the user types their method name into XojoScript, how could I have it still identified and executed? It may not be possible, but it would be desired. Any ideas?