Skip to content

Declaring Procedure Methods

A Procedure method performs a discrete operation on the object or on behalf of the object. The syntax for declaring a Procedure Method is:

Procedure {method-name} [{type1} {param1} {type2} {param2}]
    [{variable-declarations}]
    {statement 1}
    {statement 2}{statement n}
End_Procedure

Where:

  • {method-name} is the name of the procedure method. You cannot use a DataFlex reserved word as a method name.
  • {type1} is the data type of parameter {param1}, {type2} is the data type of {param2}, etc.
  • {variable-declarations} are the local variables that you can declare for the method.
  • {statement 1-n} are the statements that are executed whenever the method is called.

Parameters

You can define zero or more parameters for each procedure method. Each parameter behaves like a local variable that is initialized by the statement that executes the method. The data types of each parameter can be any of the DataFlex simple types (Integer, Number, Real, etc.), as well as struct types and arrays.

By reference parameters can also be declared. In this case, the parameter's name must be preceded by the keyword ByRef. When a parameter is passed by reference, the procedure has direct read/write access to the passed variable. For more information, refer to Parameter Passing.

Examples

Here is an example of a class definition with a few procedure method declarations:

Class cCount_Button is a Button
    // Constructor:
    Procedure Construct_Object
        Forward Send Construct_Object   // very important!
        Property Integer piClickCount 0
    End_Procedure

    Procedure DoPrint String sMessage
        Showln "Our message today is:"
        Showln sMessage
        Showln
    End_Procedure

    Procedure OnClick
        Integer iCount
        Get piClickCount To iCount
        Move (iCount + 1) To iCount
        Set piClickCount To iCount
        Send DoPrint ("Click count = " + String(iCount))
    End_Procedure

    Procedure PrintValues Integer[] values
        ...
    End_Procedure

    // Assuming a struct type tCustomer has been declared somewhere else
    Procedure PrintCustomer tCustomer cust
        ...
    End_Procedure
End_Class

The methods in this example demonstrate various procedure method declarations. The Button class is designed to execute a method called OnClick each time the button is clicked on by the mouse. The OnClick method we have defined will increment a property and then execute the DoPrint method to print the value of that property.

A Procedure method executes until it reaches an End_Procedure or it encounters a Procedure_Return command. Procedure_Return can be used to halt execution of a Procedure method at any point in the procedure body. The OnClick method in the previous example could be re-written as follows:

Procedure OnClick
    Integer iCount
    Get piClickCount To iCount
    If (iCount >= 1000) Begin
        Error 300 "You have tried too many times. Give up!"
        Procedure_Return
    End
    Move (iCount + 1) To iCount
    Set piClickCount To iCount
    Send DoPrint ("Click count = " + String(iCount))
End_Procedure