Learn about the VAR statement
- Last Updated: December 14, 2023
- 4 minute read
- OpenEdge
- Version 13.0
- Documentation
The VAR statement is an ABL statement that
allows developers to define program variables more concisely, in a manner similar to
modern programming languages. The VAR statement is a
short-hand notation for the DEFINE VARIABLE statement. With VAR, you always get NO-UNDO behavior,
multiple variables can be defined in a single statement, bracket notation is used for
defining arrays, and the equal sign (=) is used to assign initial values.
Syntax
|
For more details about the syntax, see the VAR statement in the ABL
Reference.
VAR statement features
TheVAR statement:- Is
NO-UNDO, meaning that the value of the variable will not be restored if the variable changed during a transaction and the transaction is undone. If you wish to have the capability to restore the variable to its prior value, use theDEFINE VARIABLEstatement instead. - Allows you to define multiple variables in one statement. For
example:
VAR CHAR s1, s2, s3. - Allows you to define arrays without the
EXTENTkeyword, using bracket notation instead. If the brackets are empty ([]), the array is indeterminate. For example:VAR CHAR[10] s4. //s4 is a determinate array with 10 elements VAR CHAR[] s5. //s5 is an indeterminate array - Allows you to specify initial values without the
INITIALkeyword, using an equal sign (=) instead. For example:VAR CHAR s6 = "My character string". - Allows you to instantiate an object in a
single statement. For example:
VAR myclass myobj = NEW myclass(). - Allows you to instantiate and initialize array
object variables. For example:
VAR StateClass[2] objArrayA = [NEW StateClass(“MA”), NEW StateClass(“NH”)]. VAR StateClass[] objArrayB = [NEW StateClass(“MA”), NEW StateClass(“NH”)]. - Allows you to use expressions. For
example:
VAR DATETIME dtm = DATETIME(TODAY,MTIME).
VAR statement restrictions
TheVAR statement has the
following restrictions.VARmay only be used at the beginning of an ABL routine, before any executable statements in the block.If in a procedure, theVARstatement must follow these statements:USING...BLOCK-LEVEL ON ERROR UNDO, THROW.ROUTINE-LEVEL ON ERROR UNDO, THROW.
If in a class, theVARstatement:- Must come after the
CLASSstatement. - Can be in any order within the class, like other class members.
- Must be at the top of the block within a method or property body.
VARdoes not allow abbreviated data types. For example, when specifying a decimal variable, you cannot writeVAR DEC. You must writeVAR DECIMAL. However, CHAR and INT are actual keywords and not abbreviations. CHAR is a synonym for CHARACTER and INT is a synonym for INTEGER. Therefore you can writeVAR CHARorVAR INT.- Other options allowed with the
DEFINE VARIABLEstatement, such asFORMATorLABEL, cannot be used withVAR. If you need those other options, use theDEFINE VARIABLEstatement instead.
Examples
The following examples show some of the ways you can write VAR statements along with their equivalent DEFINE VARIABLE statements.
Four character variables with default initial values:
|
|---|
|
Three integer variables (z’s initial value is 3 and x and y default to 0):
|
|---|
|
Three object variables:
|
|---|
|
Object variable with the optional CLASS
keyword:
|
|---|
|
.NET generic object:
|
|---|
|
Three date variables (d1's initial value defaults to the Unknown
value (?)):
|
|---|
|
Date variables defined with an access mode:
|
|---|
|
Three determinate arrays of size 3 (x's third element defaults to 2 (the previous element); y's elements default to 0):
|
|---|
|
Two indeterminate arrays:
|
|---|
|
Object array of size 2:
|
|---|
|
Instantiated object variable:
|
|---|
|
Instantiated and initialized determinate array object variable:
?) value, unlike scalar array variables
where uninitialized elements are set to the last initialized value, if any.
|
|---|
|
|
|---|
|
Variable initialized using an expression:
|
|---|
|