Skip to content

Broadcast

See Also: Broadcast_Focus, Delegate, Forward, Get, Send, Set

Purpose

To send a message to child objects of an object. It can also be used to send a message to all descendants of an object. When sent to the desktop, all objects in the program can receive the message.

Syntax

Broadcast [Recursive | Recursive_Up] [No_Stop] {Get | Set | Send} {message…}

Where {message…} is the name and syntax of the message to broadcast. Refer to the Get, Set, or Send commands for more information on their respective syntax.

What It Does

The broadcast command sends a message to the children of an object. The child objects receive the message in the order in which the children were first created.

  • The recursive option allows a message to be sent to all generations descended from the object, instead of just its immediate children. The message will be sent to a child and then to all of that child's children before proceeding to the next child. This option allows the message to be sent to the parent before it is sent to its children. This is useful when the parent of an object must be notified of an event before its children are.

  • If the Recursive_Up option is used, the recursion will proceed from the lowest level of descendence up to the original recipient object. This method of recursion will allow the lower levels to process the message before their parent objects process the message. Broadcast in this mode can be used to deactivate child objects, destroy child objects, set values of properties, and so forth. This mode is well suited for left-right tree navigation, where every node on the tree is an object.

  • If a recipient object is not specified in the Get, Set, or Send, all objects will receive the message.

  • When Broadcast is used with the Get command, the broadcasting will terminate when the first non-zero integer value is returned (for up to 2 levels). This is one way to build a system where the first object that answers a broadcasted request will be used.

  • The broadcast command will stop when the broadcasted message returns a value not equal to zero. With the No_Stop option, a Broadcast Get|Set|Send instruction will continue broadcasting when the message returns a non-zero value. Since only functions should return values, this is primarily true for a Broadcast Get usage. However, there are procedures in DataFlex (runtime and packages) that do return a value. So, the No_Stop option can be useful when calling functions and procedures. When the broadcast finishes, the return value will be that of the last object that processed the message.

Example

Use Windows.pkg

Class cTreeNode Is An Array
    Procedure Construct_Object
        Property Integer piLevel
    End_Procedure

    Procedure CreateNode Integer iMaximumLevels Integer iNodesPerLevel
        Integer i
        For i From 1 To iNodesPerLevel
            Object oNewNode Is A cTreeNode
                Set piLevel To (piLevel(parent(Self)) + 1)
                If (piLevel(Self) <= iMaximumLevels);
                    Send CreateNode iMaximumLevels iNodesPerLevel
                End_Object
            Loop
        End_Procedure

    Procedure VisitNode
        Showln "Visiting Node: " (Object_Id(Self)) " Level: " (piLevel(Self))
    End_Procedure
End_Class

Class cTree Is An Array
    Procedure Construct_object
        Property Integer piMaximumLevels
        Property Integer piNodesPerLevel
        Object oBaseNode Is A cTreeNode
            Set level To 1
            Showln "Creating Node: " (Object_Id(Self)) " Level: " (piLevel(self))
        End_Object
    End_Procedure

    Procedure Populate
        Send CreateNode To (oBaseNode(Self)) (piMaximumLevels(Self)) (piNodesPerLevel(Self))
    End_Procedure

    Procedure Navigate
        Showln "Recursive Up"
        Broadcast Recursive_Up
        Send VisitNode To (oBaseNode(Self))
        Showln "Recursive Down"
        Broadcast Recursive
        Send VisitNode To (oBaseNode(Self))
    End_Procedure
End_Class

Object oTest is A Button
    Object oTree Is An cTree
        Set piMaximumLevels To 3
        Set piNodesPerLevel To 3
        Send Populate
    End_Object

    Procedure OnClick
        Send Navigate To oTree
    End_Procedure
End_Object

Example

Use Windows.pkg

Object t1 is a cObject
Object t2 is a cObject

Function TestFuncOne String sParameter Returns Integer
    Showln "TestFuncOne: " sParameter
    Function_Return 1
End_Function

Object t4 is a cObject
Function TestFunc Returns Integer
    Integer iRetVal
    Move (TestFuncOne(Self, "TEST1")) to iRetVal
    Get TestFuncOne "TEST2" to iRetval
    Showln 1
    Function_Return 1
End_Function
End_Object

End_Object

Object t3 is a cObject
Function TestFunc Returns Integer
    Showln 2
    Function_Return 0
End_Function
End_Object

Integer iError
Broadcast Recursive Get TestFunc of (t1(Self)) to iError
Showln "Error: " iError
End_Object

Send Info_Box "Wait"

Notes

  • Prior to the command sending the message, the Delegation_Mode of each focus child is temporarily changed to No_Delegate_Or_Error for receipt of a Broadcast message. This is necessary so that if the child does not understand the message, it will not be delegated back to the parent, and possibly cause an endless loop. After the message returns, the Delegation_Mode is restored to its prior value.

  • As an alternative to the Broadcast command, the Broadcaster class may be used if the desired group of objects is not descended from the same parent.