Delegation

The DataFlex Send, Set and Get statements instruct an object to execute a method. We say that the object receives a message (or instruction) to execute a method. If the object does not have a definition for the method then the message will be automatically passed to the object's parent. This is repeated until either the message is resolved or the message is passed to the outer-most parent and is still not resolved. When a message is unresolved in this way an error is raised.

The process of passing an unresolved message on to the parent object is called delegation.

The following is an example of object design that takes advantage of message delegation.

Object oButtonPanel  is a Panel

    Set Size to 50 70

 

    Procedure DoClose

        Send Info_Box "Goodbye!"

        Send Deactivate

    End_Procedure

 

    Object oButton1  is a Button

        Set Location  To 10 10

        Set Label     To "Button 1"

              

        Procedure OnClick

            Send Info_Box "I am button #1"

            Send DoClose

        End_Procedure

    End_Object

 

    Object oButton2  is a Button

        Set Location  To 30 10

        Set Label     To "Button 2"

              

        Procedure OnClick

            Send Info_Box "I am button #2"

            Send DoClose

        End_Procedure

    End_Object

End_Object

When clicked, both buttons send DoClose to the current object (themselves). However neither button has a definition for the DoClose method therefore this message would be delegated and resolved by the parent object oButtonPanel.

Note that the objects in the above example define methods as part of the object definition rather than creating a sub-class. This was done for simplicity and to demonstrate how delegation occurs more clearly.

Explicit Delegation

You can explicitly delegate a message to the current object's parent by executing a Delegate statement. An explicitly delegated message directly instructs the current object's parent to execute a method. If this method is not resolved in the parent then the normal flow of message delegation will continue to the grand-parent object and so on.

All three method types can be explicitly delegated (Procedure, Procedure Set, and Function). The syntax for delegating a message is different for each type of method as outlined below:

Delegating Procedure Methods

Delegate Send {method-name}  {Param1 … ParamN}

 

Delegating Procedure Set Methods

Delegate Set {method-name} {Param1 … ParamN}  To {value1 … valueN}

 

Delegating Function Methods

Delegate Get {method-name} {Param1 … ParamN}  To {receiving-variable}

You can see that the syntax for delegating a message is the same as the syntax for executing a method except that the Delegate command is appended to the front of each statement. There is also no object reference when delegating a method. You are always delegating the message to the parent of the current object.

The following is an example of object design that takes advantage of explicit message delegation.

Object oPanel  is a Panel

    Set Size to 50 70

 

    Object oButton1  is a Button

        Set Location  To 10 10

        Set Label     To "Button 1"

              

        Procedure OnClick

            Send Info_Box "I am button #1"

            Delegate Send Deactivate

        End_Procedure

    End_Object

 

    Object oButton2  is a Button

        Set Location  To 30 10

        Set Label     To "Button 2"

              

        Procedure OnClick

            Send Info_Box "I am button #2"

            Delegate Send Deactivate

        End_Procedure

    End_Object

End_Object

In the above example, the "Delegate Send Deactivate" statement ensures that it is the object oPanel that executes the Deactivate method. If this message was not delegated then each button would try to deactivate itself when it was clicked rather than deactivate the whole panel since the Button class also understands the Deactivate method.

Refer to Methods in the Classes section for more information on calling methods.