Dynamically reference object using variable value

I am trying to understand how I can implement some of my VB code in Xojo, as I am considering moving to this:

  1. I need to be able to set the properties of an object whose name is stored in a variable,
    In VB, I would add the object to a Panel and use the following code:

         String1 = "Buton1"
         Panel1.Controls(String1).Text = "BlahBlah"
    

    I realized .Text will be .Caption in Xojo

  2. I need to be able to execute a method where the name is stored in a variable. In VB I would
    use something like:

    String2 = “App1.Module1”
    System.Reference.Assembly.Get ExecutingAssembly.CreateInstance(String2)

The reason for this, is that I build values of the strings from a database, which allows me to build standard applications just changing the entries in the DB. The majority of back end coding for screen menus of any type and application configuration can then be controlled via a database entry and not hard coded entries or even config files. All I worry about is the UI Experience/ layout. For standard database apps, I use my own metadata tables to drive maintenance and lookup utilities with zero additional coding. If I can get these working, I will given examples on how this can be done.

Any and all assistance is greatly appreciated.

You can add objects to a dictionary with their name as key. This way you can refer them later.

For controls you could build this dictionary with looping over controls and accessing them by index in a window.

Dim miList() As Introspection.MethodInfo = Introspection.GetType(window1).getmethods
For Each m As Introspection.MethodInfo In miList()
  If m.name = "Untitled" Then
    m.Invoke(Window1)
  End If
Next

This will call the Untitled method on Window1 if you add this code to a button on window1

You can add a method to the window.

Function ControlByName(name as string) as RectControl
   for i as integer = 0 to Self.ControlCount - 1
      if Self.Control(i) IsA RectControl then
         if RectControl(Self.Control(i)).name = name then return RectControl(Self.Control(i))
      end
   next
End Function

And use it like

String1 = "Button1"
PushButton(ControlByName(String1)).Caption = "BlahBlah"

Note that you would still have to have some information about the control type in order to set the right property. But you can generalize this quite a bit with things like extends methods and IsA.

Three great answers by three great Xojo heavyweights!