Start of Content Area

Declaring Field Symbols  Locate the document in its SAP Library structure

To declare a field symbol, use the statement

FIELD-SYMBOLS <fs> [typing].

For field symbols, the angle brackets are part of the syntax. They identify field symbols in the program code.

If you do not specify any additions, the field symbol<fs> can have data objects of any type assigned to it. When you assign a data object, the field symbol inherits its technical attributes. The data type of the assigned data object becomes the actual data type of the field symbol.

Note: it is possible to assign reference variables and structured data objects to untyped field symbols. However, the static field symbol is only a pointer to the field in memory, and does not have the complex type attributes of a reference or structured field until runtime. You can only use the field symbol to address the whole field (for example, in a MOVE statement). Specific statements such as CREATE OBJECT <fs> or LOOP AT <fs> are not possible.

Typing Field Symbols

The typing addition allows you to specify the type of a field symbol. When you assign a data object to a field symbol, the system checks whether the type of the data object you are trying to assign is compatible with that of the field symbol. If the types are not compatible or convertible, the system reacts with a syntax or runtime error. If you wish to perform casting of data objects using field symbols, you must do so explicitly using the ASSIGNstatement. The system then treats the assigned data object as if it had the same type as the field symbol.

You specify the type of a field symbol using the same semantics as for typing formal parameters in procedures. You can specify the type either generically or in full. If you specify a generic type, the type of the field symbol is either partially specified or not specified at all. Any attributes that are not specified are inherited from the corresponding data object in the ASSIGN statement. If you specify the type fully, all of the technical attributes of the field symbol are determined when you define it. You can then only assign data objects to it that have exactly the same data type.

You should always specify a type for each field symbol. If you cannot avoid defining a generic field symbol, make this clear by using an appropriate generic type declaration.

For more information on typing using the typingaddition, refer to the keyword documentation.

Generic Type Specification

The following types allow you more freedom when assigning data objects. The data object only needs to have the specified attributes of the field symbol.

If you specify a type generically, remember that the attributes inherited from the data object during assignment are not statically known in the program. You can, at most, address them dynamically.

Example

TYPES: BEGIN OF line,
         col1 TYPE c,
         col2 TYPE c,
       END OF line.

DATA: wa TYPE line,
      itab TYPE HASHED TABLE OF line WITH UNIQUE KEY col1,
      key(4) TYPE c VALUE 'COL1'.

FIELD-SYMBOLS <fs> TYPE ANY TABLE.

ASSIGN itab TO <fs>.

READ TABLE <fs> WITH TABLE KEY (key) = 'X' INTO wa.

The internal table itab is assigned to the generic field symbol <fs>, after which it is possible to address the table key of the field symbol dynamically. However, the static address

READ TABLE <fs> WITH TABLE KEY col1 = 'X' INTO wa.

is not possible syntactically, since the field symbol does not adopt the key of table itab until runtime. In the program, the type specification ANY TABLE only indicates that <fs> is a table. If the type had been ANY (or no type had been specified at all), even the specific internal table statement READ TABLE <fs>  would not have been possible from a syntax point of view.

If you adopt a structured type generically (a structure, or a table with structured line type), the individual components cannot be addressed in the program either statically or dynamically. In this case, you would have to work with other field symbols and the method of assigning structures component by component.

Specifying the Type Fully

If you have full types specified, the technical attributes of the field symbols are determined. The technical attributes of the assigned data objects must match those of the field symbol.

When you use a field symbol that is fully typed, you can address its attributes statically in the program, since they are recognized in the source code. If you fully specify the type of a field symbol as a reference or structured data object, you can address it as you would the data object itself, once you have assigned an object to it. So, for example, you could address the components of a structure, loop through an internal table, or create an object with reference to a field symbol.

Example

REPORT demo_field_symbols_type.

DATA: BEGIN OF line,
         col1(1) TYPE c,
         col2(1) TYPE c VALUE 'X',
       END OF line.

FIELD-SYMBOLS <fs> LIKE line.

ASSIGN line TO <fs>.

MOVE <fs>-col2 TO <fs>-col1.

The field symbol <fs> is fully typed as a structure, and you can address its components in the program.

 

End of Content Area