Pass method name as argument in a different method

Hi. I’m working on a project that has a bunch of clickable/touchable “buttons” in it. The buttons are in their own class and are registering click/touch events. When it has been determined that a button has been pressed, I’m throwing an event that my main app is registered too. I’m wondering if it’s possible to send a method name along in that event so the main app knows what should happen when the button is pressed. Instead of having a bunch of IF statements or a long SWITCH statement for each button, it would only have to be a couple of lines, just executing whatever method was passed in with the event.

Is this possible and how would I go about that?

Thanks a bundle.

You could use function pointers. They’re pretty much what they sound like: pointers to functions. Fair warning though, they’re not the most pleasant C / C++ feature around.

Here’s an in-depth tutorial on them:

There’s also the OF events api, which might be familiar to you if you’ve used Javascript or ActionScript. If you take a look in the examples/events there’s a few examples of how you might be able to leverage those events in your application. That’s a perhaps friendlier and more robust way of dealing with event handling than passing around function pointers.