Entering content frameConstants Locate the document in its SAP Library structure

Constants are named data objects that you create statically using a declarative statement. They allow you to store data under a particular name within the memory area of a program. The value of a constant must be defined when you declare it. It cannot subsequently be changed. The value of a constant cannot be changed during the execution of the program. If you try to change the value of a constant, a syntax error or runtime error occurs.

You declare them using the CONSTANTS statement. Within the program, you can also declare local variables within procedures using CONSTANTS. The same rules of visibility apply to constants as to types (see The TYPE Addition). Local constants in procedures obscure identically-named variables in the main program. Constants live for as long as the context in which they are declared.

The syntax of the CONSTANTS statement is exactly the same as that of the DATA statement, but with the following exceptions:

Example

Elementary constants:

CONSTANTS: pi TYPE P DECIMALS 10 VALUE '3.1415926536'.

           ref_c1 TYPE REF TO C1 VALUE IS INITIAL.

The last line shows how you can use the IS INITIAL argument in the VALUE addition. Since you must use the VALUE addition in the CONSTANTS statement, this is the only way to assign an initial value to a constant when you declare it.

Complex constants:

CONSTANTS: BEGIN OF myaddress,
           name(20)    TYPE c  VALUE 'Fred Flintstone',
           street(20)  TYPE c  VALUE 'Cave Avenue',
           number      TYPE p  VALUE  11,
           postcode(5) TYPE n  VALUE  98765,
           city(20)    TYPE c  VALUE  'Bedrock',
           END OF myaddress.

This declares a constant structure MYADDRESS. The components can be addressed by MYADDRESS-NAME, MYADDRESS-STREET, and so on, but they cannot be changed.

 

 

Leaving content frame