Language Elements and Reference:xpplrm

General structure of an Xbase++ program Foundation

This section shows a simple programming example illustrating the structure elements of an Xbase++ program. It begins with the declaration of a procedure having the identifier MAIN. This identifier is used for the main or startup routine of a program (meaning each Xbase++ program starts with MAIN). Within MAIN, a menu is programmed to allow the user to make a selection on the screen.

When the user has made a selection from the program, the program branches using a DO CASE control structure which calls various user defined functions and procedures.

The program code of the user-defined functions and procedures in the example merely displays a character string. The example primarily serves as an overview of structure and syntax elements of an Xbase++ program.

**************                   // Declare starting procedure, 
PROCEDURE Main                   // per convention: Main 
   LOCAL nMenuItem := 1          // Declare and initialize 
                                 // variable visible in main 
   DO WHILE .T.                  // Start infinite loop 
      CLS                        // Clear screen 
      SetColor( "N/BG,W+/B" )    // Set color for menu 
      @ 0, 0                     // Set cursor to position 0,0 and 
                                 // fill first row with blank spaces 
      SET WRAP ON                // Wrap highlight bar 
      SET MESSAGE TO MaxRow() CENTER 
                                 // Define menu 
      @ 0, 1 PROMPT " DBF file " ; 
            MESSAGE "Open and close DBF files" 

      @ Row(), Col()  PROMPT " NTX file " ; 
                     MESSAGE "Open and close Index files" 

      @ Row(), Col()  PROMPT " Search " ; 
                     MESSAGE "Find data records" 

      MENU TO nMenuItem          // Allow menu selection by user 

      DO CASE                    // Branch program according 
      CASE nMenuItem == 0        // to selection 
         ? "Terminated without selection" 
         EXIT                    // Leave DO WHILE loop 
      CASE nMenuItem == 1        // Call user-defined 
         SelectDbfFile()         // functions and procedures 
      CASE nMenuItem == 2 
         SelectNtxFile() 
      CASE nMenuItem == 3 
         QueryDbfFile() 
      ENDCASE 
   ENDDO                         // End of infinite loop 

RETURN                           // Back to operating system 

********************** 
FUNCTION SelectDbfFile           // Declare function 
   ? "Select DBF file"           // Program code of the function 
RETURN .T.                       // Return value is a literal 

********************** 
FUNCTION SelectNtxFile           // Function declaration 
   ? "Select Index file"         // Program code of the function 
RETURN (IndexOrd())              // Return value is an expression 

********************** 
PROCEDURE QueryDbfFile           // Declare procedure 
   ? "Find data record"          // Program code of the procedure 
RETURN                           // Back to calling routine 

The example program demonstrates various conventions for how to write and arrange program code for clarity. Keywords for declarations, statements and commands are written in uppercase. Identifiers are shown in mixed case within the program code.

The beginning of user-defined functions and procedures is marked within the program code using comments. When using inline comments, it has proven effective to have the comments always begin in the same column, if possible. A well documented program is divided with program code on the left side and comments on the right side.

Following statements that introduce control structures, the program code is indented a specific number of blank spaces. As soon as the control structure ends via the corresponding termination statement, the indent ends and all subsequent lines begin at the previous column. Not only is the readability of the program improved, but nested statements can be easily tested for correct syntax by simply reading the code.

When program code from several programers is being used, blank space should always be used for indentation instead of the horizontal tab. A blank space is formatted the same way by all editors, while the tab can be formatted in different ways by different editors. Indenting is supposed to improve the readability of a program, but using tabs can have the opposite effect.

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.