Progress
Language Tutorial
for Character


Using Shared Variables

When you define a variable in one procedure, you can only use it in that procedure or the internal subprocedures of that procedure. This kind of variable is known as a local variable. If you want more than one procedure to use a variable, then you need to use a shared variable. To defined a shared variable, use the DEFINE VARIABLE statement.

SYNTAX
DEFINE [ [ NEW ] SHARED ] VARIABLE variable-name
  { AS data-type | LIKE field } 

When you use a shared variable, only one procedure can own the shared variable. That procedure creates it, and when that procedure ends, so does the availability of the shared variable. The owner procedure must use the two keywords NEW SHARED in the DEFINE VARIABLE statement, as in this example:

DEFINE NEW SHARED VARIABLE Field1 AS CHARACTER. 

Any other procedure that uses the shared variable must also define it, but without the keyword NEW:

DEFINE SHARED VARIABLE Field1 AS CHARACTER. 

Note that any format phrase option, such as a label or format string, specified in the original definition (NEW SHARED), does not apply to the variable in subsequent definitions (SHARED). You can specify different options for the variable in different procedures.

Also note that you cannot define shared variables in a user-defined function.

Figure 6–5 shows the relationship among three procedures and the shared variable that two of these procedures access. The proc1.p procedure defines a NEW SHARED variable called Shareinfo and runs a procedure called proc2.p. The proc2.p procedure does not use Shareinfo but calls proc3.p, which does. Procedure proc3.p needs to define Shareinfo as a shared variable. Even though you don’t run proc3.p from proc1.p, it still can still access the shared variable. Once proc1.p defines the variable, any procedure that also defines that variable can access the data stored there.

Figure 6–5: Shared Variables


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