Callbacks, LIsteners, and Event Communication

I have a TCPSocket in my project, and I want to use it from several different parts of my code by instantiating the object like

Dim s as new AppTCPSocket
s.initializeSettings(settings)
s.connect()

But the problem is, when the TCPSocket has events that fire, I’m having a bit of a confusing time trying to figure out how to get that status back to other parts of the code. In other languages, I might add a callback to the TCPSocket’s event handler, or register a listener, or have TCPsocket send an event to another object. Something is necessary so that, when something happens to the TCPSocket, my app can respond.

I’m not sure how to do this in Xojo. I can’t seem to find any documented way to get a reference to a function (and haven’t tried yet) or call a function by name through a string variable. But I also wonder what I am supposed to do with TCPSocket events - maybe I need to rethink my object structure.

I don’t want to reference a specific control in my AppTCPSocket class. I could subclass AppTCPSocket for each type of control I want it to interact with but yuck - I really don’t want my AppTCPSocket derivatives to know anything at all about my user interface.

Any help would be appreciated, or a pointer to an example project perhaps where a non-UI control connects with several different controls of the UI.

And regardless, does Xojo support any type of callback mechanism? Can I pass a function as a parameter? That seems genuinely useful in many circumstances and is possible in every other language I use, so seems good to know.

Thanks,

Mars

There are a couple ways to do what you want. Most commonly, people will drag the control onto the Window, which will give you a a way to implement the event handlers.

Alternatively, if you want to do it all in code you can use the AddHandler command to map event handlers to methods that you specify:

http://documentation.xojo.com/index.php/AddHandler

AddHandler is also covered in User Guide Book 3: Framework, Chapter 10: Advanced Features, AddHandler section.

Have a look at delegates.
http://documentation.xojo.com/index.php/Delegate

Thanks to both of you very much. Good stuff - all. I’m glad to know about the delegates option and that’ll solve the problem of needing to implement callbacks.

Paul, when trying to drag an instance of my AppTCPSocket into my AppControl - I have a lot of controls, objects and other things in my navigator. The AppTCPSocket isn’t anywhere near the AppControl I want to drop its instance into, and the navigator doesn’t seem to scroll when I drag past the top or bottom. Is there a way to scroll the navigator while I am trying to drag a class and drop an instance?

Thanks very much for the excellent tips…
Mars

You should be able to click on the Window to show its layout and then just to scroll the the control and drag it onto the Window Layout that remains visible on the screen.

Thanks Paul. Also, I discovered that Copy and Paste works for controls, methods, and other things in the navigator… Immensely helpful!