PreviousNextIndex

DEFINE VARIABLE statement

Defines a variable for use in one or more procedures, a variable data member of a class for use in a single class or class hierarchy, or by other classes and procedures, or a variable data element for use within a single class-based method.

Syntax

DEFINE { [ [ NEW [ GLOBAL ] ] SHARED ] |  
            [ PRIVATE | PROTECTED | PUBLIC ] [ STATIC ] } 
  VARIABLE variable-name 
  { {   AS [ HANDLE TO ] primitive-type-name  
       | AS [ CLASS ] { object-type-name } 
       | LIKE field       } [ EXTENT [ constant ] ] }  
  [ BGCOLOR expression ] 
  [ COLUMN-LABEL label ] 
  [ CONTEXT-HELP-ID expression ] 
  [ DCOLOR expression ] 
  [ DECIMALS n ] 
  [ DROP-TARGET ] 
  [ FONT expression ] 
  [ FGCOLOR expression ] 
  [ FORMAT string ] 
  [ INITIAL 
      { constant | { [ constant [ , constant ] ... ] } } ] 
  [ LABEL string [ , string ] ... ] 
  [ MOUSE-POINTER expression ] 
  [ NO-UNDO ] 
  [ [ NOT ] CASE-SENSITIVE ] 
  [ PFCOLOR expression ] 
  { [ view-as-phrase ] } 
  { [ trigger-phrase ] } 

NEW SHARED VARIABLE variable-name
NEW GLOBAL SHARED VARIABLE variable-name
SHARED VARIABLE variable-name
[ PRIVATE | PROTECTED | PUBLIC ] [ STATIC ] VARIABLE variable-name
VARIABLE variable-name
AS primitive-type-name
AS [ CLASS ] { object-type-name }
object-type-name
CLASS
LIKE field
EXTENT [ constant ]
BGCOLOR expression
[ NOT ] CASE-SENSITIVE
COLUMN-LABEL label
CONTEXT-HELP-ID expression
DCOLOR expression
DECIMALS  n
DROP-TARGET
FGCOLOR expression
FONT expression
FORMAT string
INITIAL { constant | [ constant [ , constant ] . . . ] }
LABEL string [ , string ] ...
MOUSE-POINTER expression
NO-UNDO
PFCOLOR expression
view-as-phrase
trigger-phrase
Examples

The r-dfvar.p procedure defines two variables, del and nrecs to be shared with procedure r-dfvar2.p. The del variable passes information to r-dfvar2.p, while nrecs passes information back to r-dfvar.p from r-dfvar2.p.

r-dfvar.p
DEFINE NEW SHARED VARIABLE del   AS LOGICAL NO-UNDO. 
DEFINE NEW SHARED VARIABLE nrecs AS INTEGER NO-UNDO. 
MESSAGE "Do you want to delete the orders being printed (y/n)?" UPDATE del. 
RUN r-dfvar2.p. 
IF del THEN 
  MESSAGE nrecs "orders have been shipped and were deleted". 
ELSE  
  MESSAGE nrecs "orders have been shipped". 

r-dfvar2.p
DEFINE SHARED VARIABLE del   AS LOGICAL NO-UNDO. 
DEFINE SHARED VARIABLE nrecs AS INTEGER NO-UNDO. 
OUTPUT TO PRINTER. 
FOR EACH Order WHERE Order.ShipDate <> ?: 
   nrecs = nrecs + 1. 
   FOR EACH OrderLine OF Order: 
     DISPLAY OrderLine.OrderNum OrderLine.LineNum OrderLine.Qty 
       OrderLine.Price. 
       IF del THEN DELETE OrderLine. 
   END. 
   IF del THEN DELETE Order. 
END. 
OUTPUT CLOSE. 

The following example is a startup procedure. It defines a new global variable with the initial value TRUE and uses that variable to determine whether to run an initialization procedure, r-init.p, that displays sign-on messages. Then the global variable first-time is set to FALSE. If you restart this procedure during the same session (pressed STOP), r-init.p does not run again.

The procedure also defines the variable selection for entering menu choices within this procedure:

r-dfvar3.p
DEFINE NEW GLOBAL SHARED VARIABLE first-time AS LOGICAL NO-UNDO INITIAL TRUE. 
DEFINE VARIABLE selection AS INTEGER NO-UNDO FORMAT "9" LABEL "Selection". 
IF first-time THEN DO: 
  RUN r-init.p. 
  first-time = FALSE. 
END. 
FORM 
  "    MAIN MENU          " SKIP(1) 
  "1 - Accounts Payable   " SKIP 
  "2 - Accounts Receivable"  
  WITH CENTERED ROW 5 FRAME menu. 
REPEAT: 
  VIEW FRAME menu. 
  UPDATE selection AUTO-RETURN WITH FRAME sel CENTERED ROW 12 SIDE-LABELS. 
  IF selection = 1 THEN DO: 
    HIDE FRAME menu. 
    HIDE FRAME sel. 
    RUN apmenu.p. 
  END. 
  ELSE IF selection = 2 THEN DO: 
    HIDE FRAME menu. 
    HIDE FRAME sel. 
    RUN armenu.p. 
  END. 
  ELSE DO: 
    MESSAGE "Invalid selection. Try again". 
    UNDO, RETRY. 
  END. 
END. 

The following procedure finds the day of the week of a date the user enters. The procedure defines an array with seven elements and uses the INITIAL option to define the initial value of each element in the array.

r-dfvar4.p
DEFINE VARIABLE dow AS CHARACTER NO-UNDO FORMAT "x(9)" EXTENT 7 
  INITIAL ["Sunday", "Monday", "Tuesday", "Wednesday", 
  "Thursday", "Friday", "Saturday"]. 
DEFINE VARIABLE dob AS DATE NO-UNDO INITIAL TODAY. 
REPEAT WITH SIDE-LABELS 1 DOWN CENTERED ROW 10 TITLE "Date of Birth": 
  DISPLAY SKIP(1). 
  UPDATE dob LABEL "Enter date of birth". 
  DISPLAY dow[WEEKDAY(dob)] LABEL "It was a". 
END. 

The following example defines a variable with a VIEW-AS phrase and a Trigger phrase:

r-defsel.p
DEFINE VARIABLE ix    AS INTEGER   NO-UNDO. 
DEFINE VARIABLE clubs AS CHARACTER NO-UNDO 
  VIEW-AS SELECTION-LIST SIZE 20 BY 5 MULTIPLE SCROLLBAR-VERTICAL NO-DRAG 
    LIST-ITEMS "One Iron", "Two Iron", "Three Iron", "Four Iron", 
               "Five Iron", "Six Iron", "Seven Iron", "Eight Iron", 
               "Nine Iron", "Pitching Wedge" 
  LABEL "Golf Clubs Available" 
  TRIGGERS: 
    ON GO DO: 
      IF SELF:SCREEN-VALUE <> "" THEN 
      DO ix = 1 TO NUM-ENTRIES(SELF:SCREEN-VALUE) : 
        DISPLAY ENTRY(ix, SELF:SCREEN-VALUE) FORMAT "X(16)"  
          WITH FRAME clubs-sel CENTERED  
            NUM-ENTRIES(SELF:SCREEN-VALUE) + 1 DOWN 
            TITLE "Clubs Selected" USE-TEXT. 
        DOWN 1 WITH FRAME clubs-sel.  
      END. 
    END. 
  END TRIGGERS. 
     
ENABLE clubs WITH FRAME get-info TITLE "Select the Desired Club(s)". 
WAIT-FOR WINDOW-CLOSE OF CURRENT-WINDOW. 

For examples of instance and static variable data member definitions, see the descriptions of r-CustObj.cls, r-CustObjStatic.cls, and r-CustObjAbstract.cls in the CLASS statement reference entry.

Notes
See also

Class-based data member access, DEFINE BUFFER statement, RUN statement, Trigger phrase, Type-name syntax, USING statement, VIEW-AS phrase


OpenEdge Release 10.2B
Copyright © 2009 Progress Software Corporation
PreviousNextIndex