DELEGATE (function reference)

Top  Previous  Next

 

DELEGATE

 

A DELEGATE is equivalent to function pointers (i.e., callback functions), but object-oriented and type safe. Type safe means that if the function signature implemented by the Client differs in terms of number of parameters or parameter types or return values as opposed to the method signature, it is going to safely raise an error.

So a Delegate provides the feature of Callback functions in a safe way. In Clarion#, the DELEGATE keyword is used in the MAP prototype, and the EVENT keyword is also needed for the target method. The delegate is actually declared as a PROCEDURE DELEGATE in the PROGRAM MAP structure.

 

In summary, a delegate is a managed type that declares a managed pointer to a procedure, and in Clarion# it is declared in the same way that procedure type are declared in Clarion Win32.

 

Conceptual Example:

 

   PROGRAM

 

   NAMESPACE(MyNS),PUBLIC

   USING(System)

   USING(System.Collections)

 

MAP

  MyDelegate(Object sender, EventArgs e),DELEGATE,PUBLIC     !Delegate declaration

END

 

MyClass CLASS,TYPE

CONSTRUCT PROCEDURE(),PUBLIC

MyEvent   EVENT,MyDelegate,PUBLIC                         !Event declaration of type MyDelegate

OnMyEvent PROCEDURE(Object sender, EventArgs e),PUBLIC    !EventHandler procedure with the same prototype of the declared delegate MyDelegate

       END

 

CODE

 

MyClass.CONSTRUCT PROCEDURE()

CODE

    SELF.MyEvent += SELF.OnMyEvent                        !Attach the EventHandler to the event

 

MyClass.OnMyEvent PROCEDURE(Object sender, EventArgs e)

CODE

 

See Also: EVENT