Delegate basic understanding

I have a current project where I have created a few Delegates due to the fact that the design did not call for an Interface

I admit I’m not an OOP concept guru but I just want to ask if my understanding is correct

We can Invoke the Delegate as it is a like a function pointer(like in C)?

Instead of using an Interface and Instantiating a Class to use an Interface we can use a Delegate?

Thanks for any input in helping me understanding this concept in Xojo

1 Like

A delegate is like a class event but decoupled. As long as the definition (signature) is equal you can assign it to a delegate.

There are some examples of using it but not much.

Basicly it’s a description of a method (with a certain signature) that you can then use as a property, which can be invoked at will.

Is there a specific you want to know?

1 Like

Thanks for the reply

Well I guess I was just trying to wrap my head around why to use a delegate

To me it would seem one use would be because the design of the application does not require an Interface or if the design used a method outside of the Interface

Like I said I’m not a guru at all of the OOP concepts :smile:

One very common use of delegates is for adding handlers for the events of controls instantiated in code. It’s the only way you can use timers, sockets, and such that you create on-the-fly as opposed to dragging them into the IDE at design time where you can create event handlers in the IDE.

One of my apps uses text files to determine which of its methods are to be executed and in what sequence. The text file contains a list (actually XML) of the method names in the desired order of execution. The app has a dictionary whose keys are the method names and whose values are delegates for the actual methods. The app loops through the list, looking up and invoking each delegate in turn.


Very interesting
I would never have thought of using Delegates in that way

So basically a delegate (and I’m talking generally here not tied to Xojo)
is a data type definition without the need for a Interface and to Instantiate a class?
Like a function pointer?



:smile: Thanks!

You’re thinking of handlers managed with AddHandler and RemoveHandler. Delegates are similar, but essentially allow passing a method signature as a value. You might want to pass a class that says “when you’re done, execute this method.”

Thanks to handlers, delegates don’t get a ton of use. It’s often nicer to define an event then attach a handler, than it is to define a delegate and pass a signature around. But there are still use cases.

Both handlers and delegates are great ways to trigger exceptions too. They are surprisingly hard to get right.


Well, there’s no “thinking of” involved; I specifically described this use of delegates. AddHandler and RemoveAddHandler require a delegate as their second argument. The OP asked what are some uses of delegates and that’s one.

1 Like

No, they do not. AddressOf and WeakAddressOf are used with both handlers and delegates, but they are different things.


One situation where I find delegates useful is when two kinds of projects need to handle the same event from a common class.
Imagine an updater (it checks for updates of the running app, installs it and asks the user to restart the main app). This updater is a class (e.g. CUpdater).

Now, you may use this updater class in desktop and console apps. The updater may also inform the user about encountered errors.
For desktop apps, you may want to show a MessageBox (either “We’re ready to launch the updated version. Click “OK” to proceed.” or “An error occurred: [error]”) while on console apps, you’d print the message and wait for user input.

Using delegates, your desktop and console apps can each have a method to handle messages from the updater. The updater only has a delegate (with a string as a parameter) so it doesn’t know where the message will arrive nor does it even need to know anything about the rest of the app (no dependency about existing classes or methods given on which project it’s used). It’s up to the desktop and console apps to define this delegate to the correct method, like: MyUpdater.NotificationDelegate=AddressOf MyNotificationMethod.

1 Like

We hava a couple of classes that have events and delegates. Since a delegate is a class like property
you can check for Nil. So these classes have a delegate property say .onChanged As changedDelegate but also an event called Changed. When the delegate is implemented (e.g. not nil) it’s invoked otherwise the event is called.

Useful if one wants to add a quick callback to any method with the same signature.
It’s not often used this way but it’s handy.

Delegates are kinda messy in the ide, there is a menu option to add Method → From delegate but it’s kinda akward way to add delegates. I wish there where more ways, like from the navigator “add method” - “From Delegate” and then be able to select any delegate from the project (alphabetic) but currently it’s hard to find which delegates there are and where they are used in.

Events are much more clear since the are attached to a class already.


Another cool thing is you can have a dictionary with keys as string and values are the delegates (addresses).

In Xojo web we have a handler class where you register the path to a delegate. When the path is called trough app.handleURL and dict.HasKey(request.path) then we invoke the delegate (if not nil).

This is very fast, way faster than checking an array or doing alot of if request.Path = something especially when you have lot’s of paths. It’s also a perfect way to create dynamic paths or links that are for one time use (self unregistering) like download links, login links etc.