Skip to content

Sending a Message to Another Object

When a message is sent from one object to another, the destination object’s handle must be obtained. This is done by moving the object’s ID to a handle and using that variable as the object ID.

Move oSomeObject to hoHandle
Send MyMessage of hoHandle

Example

For example:

Procedure DoAction
    Handle hoButton1
    Handle hoButton2
    String sLabel1
    String sLabel2

    Move oButton1 to hoButton1   // contains object ID of button1
    Move oButton2 to hoButton2   // contains object ID of button2

    Get Label of hoButton1 to sLabel1
    Get Label of hoButton2 to sLabel2

    Send GenerateNewLabel of hoButton1 sLabel1
    Send GenerateNewLabel of hoButton2 sLabel2
End_Procedure

In the above example, object handles were moved to a variable, and the variable was then used in the message. The Move command is used to move the object’s handle to the variable. This command actually generates an internal expression that generates a handle for the object and assigns it to the variable. The method used to generate this handle will be discussed shortly.

Direct Object Reference

Alternatively, you can reference the object name directly within the message, bypassing the need to create a handle variable. This sample could have been rewritten as:

Procedure DoAction
    String sLabel1
    String sLabel2

    Get Label of oButton1 to sLabel1
    Get Label of oButton2 to sLabel2

    Send GenerateNewLabel of oButton1 sLabel1
    Send GenerateNewLabel of oButton2 sLabel2
End_Procedure

This method requires less code and is easier to read.

Optimization Considerations

While this second method makes it easier to write code, there is an optimization issue that must be understood. When an object name is used directly within a message, its handle must be evaluated each time it is used (just like the object handle must be evaluated in the Move command). This evaluation is not as efficient as using a handle variable.

So, if the same object is going to be accessed many times within the same method, you will probably want to move the object handle to a variable and then use the variable to send the message. This way, the object handle only needs to be evaluated one time. For example:

Procedure DoAction
    Handle hoArray
    Integer iCount

    // Non-Optimized: this method will be slower
    Send delete_data of oMyArray1
    For iCount from 0 to 10000
        // oMyArray must be evaluated each time it is used
        Set value of oMyArray1 iCount to "Test"
    Loop

    // Optimized: this method will be slightly faster.
    Move oMyArray2 to hoArray    // oMyArray2 is evaluated once
    Send delete_data of hoArray
    For iCount from 0 to 10000
        Set value of hoArray iCount to "Test"
    Loop
End_Procedure

The optimization involved here is quite small. In most cases, you will not be able to perceive a performance difference between the two techniques.