Create sub/function at runtime?

is there any way to inject code into a control’s events at runtime?
Or, is there a way to create a function or sub procedure and create its methods, parameters, and insert code at runtime?

Not in any conventional sense. There is XojoScript, which can be compiled at runtime, but it has a very limited interaction model with the rest of your code.

What are you trying to accomplish?

curious about that too…

the main utility is to create controls at runtime and populate the code of their events based on the data they will host or change the behavior (change of the event code) of some controls without being obliged to foresee everything in the control code in advance

Yeah, not really. I’d suggest you break down your functionality into groups of functionality and then implement it as subclassed controls that you instantiate at runtime.

2 Likes

You could use one set of controls and change the captions as needed. The action event

would then look at the caption and call the appropriate function.

1 Like

How you going to add code to be run at runtime, when the app is running? Might as well modify the app when a new situation occurs.

2 Likes

It would be much, much better to add a property to a control subclass that controlled what the button does. It’s never a good plan to base your code flow on human readable text, as it may change during translation to other languages.

It might be worth mentioning that having an application which changes it’s code at runtime might be considered malware. By default Apple’s security doesn’t like JIT compiling, you have to add special entitlements to your Mac application to use JIT with modern versions of the macOS.

I don’t know your entire situation, so this may not apply, but I would construct your code to handle the situations you know about now, while at the same time, making the code warn you (and the customer) about situations that you don’t know.

select case situations
case sitA
case sitB
case sitC
else
  break
  // ---- Provide an easy way for the customer to report this back to me.
end select
1 Like

this is just what I don’t want to do …
insert the conditional code (situation) checks in controls

Then, you will fall into the “malware” trap as Sam said above…

A full open door for bad guys to change your application behavior…

here is some quick and dirty example of doing this using xojoscript
made by a former famous member of this forum.
hope this helps.

example

2 Likes