From Xojo Documentation
A Delegate data type is an object representing a specific method.
A Delegate data type is an object representing a specific method. It is a function pointer with a method signature.
Delegates decouple interface from implementation in a similar way to events or interfaces. This decoupling allows you to treat a method implementation as a variable that is changeable based on run-time conditions. They represent methods that are callable without knowledge of the target object. You can change the function the delegate points to on the fly.
A Delegate can be declared in either a module or a class. You use the Project > Add > Delegate menu command or the (optional) Add Delegate button in the Code Editor to create a Delegate entry.
A delegate must have a name and can have optional parameters and a return type.
For use in XojoScript code:
Delegate Sub name [(parameterList)]
Delegate Function name ([parameterList]) As type
|name||Required. The name of the delegate.|
|parameterList||Optional list of values representing parameters that are passed to the method when it is called. Multiple parameters are separated by commas.|
|type||Optional. The data type of the value returned by the function.|
A Delegate is a function pointer with a method signature. It has a single method, “Invoke” whose parameters and return value match the Delegate’s parameters and return type. The Invoke method calls the method the delegate instance represents.
|While delegates are objects, you cannot create a subclass of a delegate type.|
Creating a Delegate Value
Once you have added the Delegate as described above, create a variable or property of your delegate type. For example, if you named your Delegate MyDelegate, you would declare a variable or property with myProp As MyDelegate.
There are two ways to create values that can be stored in delegates:
The other way is to specify a function pointer address of type Ptr, passing it to a delegate constructor. Assuming that there's a Delegate declared as Sub SimpleProc(), this could work as follows:
Suppose you've added a Delegate named MethodCaller with no parameters or return type, and there is a checkbox MethodCheck that will determine which method to use.
If MethodCheck.Value Then
callMethod = AddressOf TestMethod
callMethod = AddressOf AnotherMethod
If you've defined your Delegate with parameters and a return type, the code might look like this instead:
See the Delegate.Invoke method for more examples.
A Delegate is a good way to allow one object to send the same message to various other objects. You can define a Register method that takes the Delegate as a parameter and adds it to an array of your Delegate type.
When you need to send a message, you can call each Delegate in the array in a loop.
For Each d As MessageDelegate In MessageDelegateArray
If d <> Nil Then
You should also provide a method to Unregister a Delegate to remove it from the array.
Var, Static, Declare statements; -, +, *, /, <, <=, =, >=, >, <> VarType functions; AddHandler, Boolean, Byte, CFStringRef, Color, CString, Currency, Double, Int16, Int32, Int64, Int8, Integer, OSType, PString, Ptr, Short, Single, String, UInt16, UInt32, UInt64, UInt8, Variant, Window.Handle, WString data types. IsNumeric, Mod, Str, Val, Vartype, functions.