Pass Delegate Into Method

Is it possible to pass a delegate into a method like this?

Sub GetInfoForMethod(sender as TheSender) As String
#pragma unused TheSender

Return "My string expression"
End Sub

Sub MyCodeToUsePassedDelegate
SomeModule.DoStuffUsingPassedDelegate(AddressOf GetInfoForMethod)
End Sub

Dub DoStuffUsingPassedDelegate
msgbox GetInfoForMethod.Invoke
End Sub

The code doesn’t give a good example of how I would use this in a real-world app but it at least shows what I’m trying to achieve. I understand that this can be done using classes but is there I way I can avoid classes to achieve this?

Thanks

I’m using Introspection to achieve this. Any suggestions are still welcome. Thanks

Dead happy! :slight_smile: My graphics layering system works!!

You can pass anything that’s an object or type as a parameter.

Delegates are really cool once you get your head around them. Don’t forget you can call Delegate.Invoke…

[quote=208197:@Jon Ogden]You can pass anything that’s an object or type as a parameter.

Delegates are really cool once you get your head around them. Don’t forget you can call Delegate.Invoke…[/quote]
What kind of syntax do you use to pass a delegate into a method? Introspection is really cool and for the design of my code probably better than using a delegate but I still want to understand how to use it.

Well, you first create your delegate. Then in your properties of the object where the delegate resides, you create a property of the type of your created delegate. Now you can pass that property as a parameter of the method. I use it in code to create flexible event handlers for certain controls. Works really well.

Thanks

Private Delegate Function delDataAvailable(sender as IPSwitchCommands) As Boolean

So that’s my delegate signature.

Now here is my property:

Private DataAvailableCallback As delDataAvailable

Here it is passed as a parameter:

Sub SetDataAvailableHandler(theCallback as delDataAvailable)
  DataAvailableCallback = theCallback
End Sub

Adding the initial handler…

AddHandler me.DataAvailable, AddressOf HandleDataAvailable

Here’s the handler function…

Private Function HandleDataAvailable(sender as IPSwitchCommands) As Boolean
  If DataAvailableCallback <> nil Then
    Return DataAvailableCallback.Invoke(self)
  End If
End Function

Now we set up the delegate by calling the SetDataAvailableHandler method:

IPSwitch.SetDataAvailableHandler(AddressOf IPSwitch_DataAvailable)

IPSwitch_DataAvailable is a method. This handles the DataAvailable events from a serial socket.

[quote=208213:@Jon Ogden] Private Delegate Function delDataAvailable(sender as IPSwitchCommands) As Boolean

So that’s my delegate signature.

Now here is my property:

Private DataAvailableCallback As delDataAvailable

Here it is passed as a parameter:

Sub SetDataAvailableHandler(theCallback as delDataAvailable)
  DataAvailableCallback = theCallback
End Sub

Adding the initial handler…

AddHandler me.DataAvailable, AddressOf HandleDataAvailable

Here’s the handler function…

Private Function HandleDataAvailable(sender as IPSwitchCommands) As Boolean If DataAvailableCallback <> nil Then Return DataAvailableCallback.Invoke(self) End If End Function [/quote]
Thanks. I think if I follow these instructions when this kind of thing comes in handy I will get a good understanding of how to use it.

I’m not an expert on this at all and I had to look multiple times at my code to remember what I did. I got all this info from someone else here on the forum - I forget who. Search for Delegates or something like that to find more info. Really powerful feature of the language.

Thanks