Interfaces and events

There’s a question about why interfaces dont let you add event definitions to them.

In fact since they just define a signature and leave the implementation else where I suppose they could. There’s no real compelling reason they couldn’t that I can think of.

They just don’t.

And so in the mean time what do you do ? How can you have the effect of an event without being able to define one ?

What you can do is define a method on the interface that takes a delegate and then implementors can call that delegate back at appropriate times – much like you’d do with raising an event.

The upside to using an interface is that you CAN apply it to disparate items – they dont have to all inherit from the same base class or control.

The downside is that its not an event so you can use addhandler to handle it. To be quite frank some of the bugs I’ve seen over the years suggest that AddHandler is overused.

6 Replies to “Interfaces and events”

  1. Two things about Xojo interfaces (which I use a lot) irritate me:

    1. Not being able to add Notes or any other form of documentation to define what the interface methods do.

    2. Not being able to determine if a class implements one or more interfaces without opening and scrolling down the checklist of possible interfaces. I mean all they need to do is annotate the class inspector with “This class implements n interfaces”, or even better “This class implements: InterfaceA, InterfaceQ”. I suggested this via Feedback, but since it’s not a show-stopping bug, I hold out no hope.

    1. if you hover over the class in the navigator there _should_ be a tooltip that shows that

      notes _should_ be possible if you file a feature request 😛

  2. Huh. I didn’t know that (hovering to reveal interfaces). However, I consider that less than optimal. I did submit a feature request sometime in the past.

    I just submitted a feature request for Notes. I guess I thought is was so darn obvious that the Xojo developers would’ve already considered it.

Comments are closed.