Delegate property

Hi

Consider this line of code…

AddHandler DataAvailable, AddressOf CustomDataHandler

What I want to do is save CustomDataHandler in a property.

This is what I want to write…

me.MyPtr = AddressOf CustomDataHandler
AddHandler = DataAvailable, me.MyPtr

Clearly that doesn’t work and the compiler error suggests I might want a delegate

Type mismatch error. Expected delegate Delegate( MyClass ), but got Ptr.

And that is where I get lost.

How would I go about declaring a property to contain a delegate that I can use as the target of AddHandler ?

Thanks

Yes thanks.

Do you know how to do it or just signposting?

I already read the documentation. Searched the forum and looked at the examples.

Maybe it’s because it’s late, maybe because there’s a bunch of extraneous stuff in the documentation. Either way it’s not jelling.

  1. Insert a new Delegate into your app (that is, choose “Delegate” from the “Insert” menu in the IDE. Name it something relevant, like “MyDelegateType”
  2. In the new delegate type define the parameters and return type of the method you want to point to.
  3. Change the datatype of the MyPtr variable from Ptr to the newly created delegate type. You can then store the address of the method in the property and use the property with AddHandler

Here’s an example project that uses a delegate to hold the handler method of a Timer’s Action event:
delegate example.zip (2.1 KB)

2 Likes

I’d like to make a suggestion. If you’re going to go to all the trouble of creating a delegate, don’t use AddHandler. One of the great benefits of delegates is that you know that it’s been implemented by whether it’s nil. AddHandler negates that

5 Likes

Hi Greg

The purpose of the delegate is to hook the DataAvailable event handler on a socket. Can I do that without using AddHandler?

Hi Andrew.

Just wanted to express my thanks.

The 1, 2, 3, and example was exactly the clear, concise instruction I needed.

1 Like

Here’s what I do in these situations:

I subclass the class that I want to use a delegate. Create a delegate whose signature matches the event. Add a property whose type is the delegate.

Implement the event with code like this:

If DataAvailableCallback <> Nil then
DataAvailableCallback.Invoke
End If

Then changing the target is easy and you don’t need to know if you’ve added or removed a handler before. If you want it to call more than one? Make it an array to work with. It’s just so much more flexible and all encapsulated in one place.

3 Likes

This can all be done with class interfaces as well, and you get various other object-oriented advantages with them. Is there a performance advantage to using a Delegate, or some other bonus I’m missing out on? :grin:

Interfaces and delegates are not the same thing

Yeah, thanks, I wasn’t saying that. A Delegate is more flexible in some ways. But for this use case, a class interface would work quite nicely to allow the socket’s code to notify other object that data is available, etc. I use this pattern frequently. Is there a reason to not use a class interface here? I’ve always suspected there might be a performance advantage but never tested it.

Well for one thing, a class interface means that you have to have another class to apply it to. Delegates can point to methods in a module. They also let you encapsulate all of the functionality right into the object that needs the delegate(s). My favorite thing by far is that if you create a new instance of a class containing a delegate property, you can right-click on the class name, choose Add Method > From Delegate > delegateName. The IDE will automatically create a method in the object you are working on with a name _ and the exact right signature that you need.

Interfaces are great if you want to interact with a bunch of different classes that are not related to one another, and did you know that the interface methods can be private and still work if you access them through the interface? It’s a great way to “hide” functionality from classes that don’t know or care that your class has an interface applied.

2 Likes