Progress
Language Tutorial
for Character


Using Variables

A variable is a temporary storage place for data. The value contained in a variable may change during the execution of the program that contains it. Typically, variables contain data from the user, copies of database fields or other variables, or the results of expressions.

Defining Variables

The partial syntax for the define statement below contains more options than you encountered in Chapter 3.

SYNTAX
DEFINE VARIABLE variable-name 
  { AS datatype | LIKE field }
  [ EXTENT n ]
  [ FORMAT string  ]
  [ LABEL string [ , string ] ]
  [ INITIAL { constant | [ constant [ , constant ] ... ] } ] 

Table 5–1 describes the new syntax components of the DEFINE VARIABLE statement:.

Table 5–1: Syntax Components of DEFINE VARIABLE
Component
Description
AS datatype
Specifies the data type of the variable. Valid data types include: CHARACTER, INTEGER, DECIMAL, DATE, LOGICAL and WIDGET-HANDLE
LIKE
Copies the attributes of an existing field or variable. When you use LIKE, the new variable gets the data type, format, label, extent, and initial value specification of the target field or variable.
EXTENT
Makes a variable an array. Specify an integer after the keyword to indicate the number of elements in the array.
FORMAT
Specifies the data format of the variable. If you do not specify FORMAT, the variable uses the default format for its data type.
LABEL
Specifies the display label for the variable. For arrays, if you specify a single label, then every element of the array uses that label. You can also specify a different label for each element of the array using a comma-separated list of labels after the keyword.
INITIAL
Gives the variable an initial value. For arrays, if you specify a single value, then every element of the array has that initial value. You can also specify a different initial value for each element using a comma-separated list of values enclosed in brackets.

Using LIKE

When defining a variable, you must specify either the data type or a database field or another variable whose format and attributes you want to copy to the new variable. When you specify the LIKE option, the new variable acquires the format, label, and initial value attributes of the referenced field or variable. You can override an inherited attribute by using the FORMAT, LABEL, and INITIAL options.

NOTE: To use a LIKE phrase that references a database field, you must be connected to the database that contains the field you want to copy.

Examine the following code:

/*1*/  DEFINE VARIABLE New-Name1 AS CHARACTER LABEL "NAME" FORMAT "X(20)" 
                             INITIAL "All Around Sports".

/*2*/  DEFINE VARIABLE New-Name2 LIKE New-Name1.

/*3*/  DEFINE VARIABLE New-Name3 LIKE customer.name. 

Here’s what happens:

  1. You create a variable to hold the name of a company.
  2. Your second variable is identical to the first; that is, New-Name2 has the same value, format, and label that you specified in the New-Name1 definition.
  3. In this statement, you create a third variable that inherits many of the attributes of a database field. Notice the general syntax for making a specific reference to a database field: table-name.field-name. The period separates the table and field names.
Using Arrays

Arrays are fields or variables that contain multiple data elements. The extent of an array is the number of elements it contains.

To define a field or variable as an array, specify the extent. If you want to define a database field as an array, use the extent option in the Data Dictionary. To define a variable as an array, use the EXTENT option in the DEFINE VARIABLE statement.

To initialize the array with values, you can use the INITIAL option with the values listed between square brackets as shown in the following code:

DEFINE VARIABLE Letters AS CHARACTER EXTENT 3 INITIAL ["a", "b", "c"]. 
DEFINE VARIABLE Numbers AS INTEGER EXTENT 5 INITIAL [1, 2, 3, 4, 5]. 

If you do not supply enough values to fill up the elements of the array, Progress copies the last specified value into the remaining elements of the array. If you supply too many values, Progress returns an error message.

Within a procedure, you can:

Follow these steps to see how to set up an array, present the whole array for user interaction, and reference the array in a trigger:

  1. Open lt-05-01.p and run it. The interface shown below appears:
  2. The array of 12 elements contains the number of days in each month. Each element has an individual label that corresponds to the name of the month.

  3. Press TAB to move through the array elements. Notice the message area of the window. As you enter each element, the message references the label, data, and index of the element.
  4. Choose Exit, then press SPACEBAR to return to the Procedure Editor.

Here is the code that created the display:

lt-05-01.p
      /**********  DEFINE WIDGETS  **********/
/*1*/  DEFINE VARIABLE Months AS INTEGER EXTENT 12 
/*2*/      LABEL "January", "February", "March", "April", "May", 
              "June", "July", "August", "September", "October", 
              "November", "December" 
/*3*/      INITIAL [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].
      DEFINE BUTTON btn-Exit LABEL "Exit". 

      /**********  DEFINE FRAMES  **********/
      DEFINE FRAME Frame1
/*4*/      Months COLON 11 SKIP(1) 
           btn-Exit
            WITH SIDE-LABELS NO-BOX CENTERED THREE-D.
        
      /**********  DEFINE TRIGGERS  **********/
/*5*/  ON ENTRY OF Months
       DO:
/*6*/      MESSAGE SELF:LABEL "has" SELF:SCREEN-VALUE "days." 
                "The cursor is in array element number" SELF:INDEX.  
       END. 
      /**********  MAIN LOGIC  **********/
      DISPLAY Months WITH FRAME Frame1.
      ENABLE ALL WITH FRAME Frame1.
      WAIT-FOR CHOOSE OF btn-Exit. 

NOTE: The THREE-D option is relevant only on a Windows client; it is ignored by a character client.

These notes describe the array techniques used in this procedure:

  1. The DEFINE VARIABLE statement with the EXTENT option defines the array.
  2. To give each element an individual label, use a comma-separated list of strings after the LABEL keyword.
  3. To initialize the elements, use a bracketed, comma-separated list of values after the INITIAL keyword.
  4. An unsubscripted reference to an extent variable references all of the array elements. In a DEFINE FRAME statement, Progress includes room in the frame for each element of the array as a fill-in field.
  5. When you use the COLON option of the format phrase, Progress positions the field by the colon that terminates the widget label. When used with an array, the COLON option stacks the elements neatly.

  6. Here, you use the array variable name (Months) as a reference to the whole array. If the user enters a fill-in field associated with any of the elements, the trigger executes.
  7. The SELF keyword is called a system handle. The LABEL, SCREEN-VALUE, and INDEX keywords are widget attributes. A system handle is a global variable that holds a widget handle. The system maintains system handles for you. SELF is a system handle that holds the widget handle of the current widget (a widget that has input focus or a widget for which a trigger is firing). SELF is a convenient way of referring to an individual array element without having to keep track of the index. You’ll learn more about SELF later in the tutorial.
  8. The LABEL attribute holds the widget label. The SCREEN-VALUE attribute holds the value shown on the screen, which may not be the same as the value in the underlying variable. (Perhaps the user changed the screen value or the procedure changed the field value.) The tutorial covers screen values and field values more thoroughly later. Finally, the INDEX attribute holds the index value of the array element where the cursor currently resides. If you need to know explicitly where the cursor is, as opposed to relying on SELF, the INDEX attribute is your tool.


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095