Event handling within class properties

Not sure if this is just my way of working in Xojo or there is a better way to do it buuut… I would love to be able to define events in my classes and then subscribe to those events when I’m using these classes as another class property.

For example:

  1. I have ClassFoo defining an event FooEvent
  2. I have another class, ClassBar, which has a property “foo”, of type ClassFoo
  3. Instead of adding handlers inside the constructor programatically, it would be nice to be able to right click on the property, just like in other places of the IDE, and handle the event from there

This would also require to be able to instantiate these properties as an option, like the “Implicit Instance” option in forms or pages.

Does makes sense? is there a better way?

Thanks! =)


can you do this now ? no
could you - I suppose the IDE could be altered to make this possible

  1. it would need to know how “implicit instance” should work and which constructor of this class it should call (and then how or what parameters to provide)
  2. it would still more or less be adding an “addhandler” at some point to handle the event in the class that holds this property
  3. does it use weak address of etc (and if so there are issues there since the class raising the events cant tell if the item that holds the event handler actually is nil of not)

so "is it possible ? " - I would suppose so in some way
likely in the short term ? I would say unlikely

As a first go, I guess, if you want to use this feature, your class is required to provide a plain constructor, without any parameter. Otherwise you have to do it programatically (that’s fine, it’s the way you have to do it at the moment anyway ¯\_(ツ)_/¯).

Then, for the weak address and so on, I think the IDE just have to follow the same rules it uses in the other places where you can add handlers visually.

And yep, for sure, this is just another feature request, for the long term, if it makes sense.

Would it work to make your classes subclasses of ContainerControl? You wouldn’t need any UI elements, or even really need to embed them on the window. But the IDE would expose the event structure for you.

I guess it would come down to how often such a configuration is used & requested as to whether they’d do it

Nice idea @Tim_Hare =), it doesn’t work for console apps but I’ll definitely give it a try to see if it’s a valid workaround for the way I use Xojo.

@Norman_Palardy here is the thing, if nobody asked for this before, it makes me think I’m trying to overuse the events or something. The reason is I’m trying to chain two or three shell command outputs, for example, in asynchronous mode, like using async/await in other languages, but probably it’s just better to put everything inside a thread and raise the last event when it finish.

Sounds like you might want to use Interactive mode instead of Asynchronous.

Thanks guys, I’ll give all this a try.