Working with Field Symbols

An important aspect of ABAP, is the ?Field Symbol?. This SAP Higher Education tip de-mystifies Field Symbols.

An important, but frequently misunderstood aspect of ABAP, is the "Field Symbol".

But you'll find they aren't mysterious. In fact, they may remind you of some features in popular general-purpose programming languages. Field symbols allow you to: ** Assign an alias to a data object(for example, a shortened name for data objects structured through several hierarchies - <fs>-f instead of rec1-rec2-rec3-f) ** Set the offset and length for a string variably at runtime ** Set a pointer to a data object that you determine at runtime (dynamic ASSIGN) ** Adopt or change the type of a field dynamically at runtime ** Access components of a structure ** (from Release 4.5A) Point to lines of an internal table (process internal tables without a separate work area) Field symbols in ABAP are similar to pointers in other programming languages. However, pointers (as used in PASCAL or C) differ from ABAP field symbols in their reference syntax. The statement ASSIGN f to <fs> assigns the field f to field symbol <fs>. The field symbol <fs> then "points" to the contents of field f at runtime. This means that all changes to the contents of f are visible in <fs> and vice versa. 

You declare the field symbol <fs> using the statement FIELD-SYMBOLS: <fs>. Reference syntax Programming languages such as PASCAL and C use a dereferencing symbol to indicate the difference between a reference and the object to which it refers; so PASCAL would use p^ for a pointer instead of p, C would use *p instead of p. ABAP does not have any such dereferencing symbol. ** In PASCAL or C, if you assign a pointer p1 to a pointer p2, you force p1 to point to the object to which p2 refers (reference semantics). ** In ABAP, if you assign a field symbol <fs1> to a field symbol <fs2>, <fs1> takes the value of the data object to which <fs2> refers (value semantics). ** Field symbols in ABAP are always dereferenced, that is, they always access the referenced data object. If you want to change the reference yourself in ABAP, you can use the ASSIGN statement to assign field symbol <fs1> to field symbol <fs2>. Using field symbols You declare field symbols using the FIELD-SYMBOLS statement. They may be declared either with or without a specific type. At runtime you assign a field to the field symbol using the ASSIGN statement. All of the operations on the field symbol act on the field assigned to it. When you assign a field to an untyped field symbol, the field symbol adopts the type of the field. If, on the other hand, you want to assign a field to a typed field symbol, the type of the field and that of the field symbol must be compatible. 

A field symbol can point to any data object and from Release 4.5A, they can also point to lines of internal tables. The brackets (<>) are part of the syntax. Use the expression <fs> IS ASSIGNED to find out whether the field symbol <fs> is assigned to a field. The statement UNASSIGN <fs> sets the field symbol <fs> so that it points to nothing. The logical expression <fs> IS ASSIGNED is then false. The corresponding negative expression is IF NOT <fs> IS ASSIGNED. An unassigned field symbol <fs> behaves as a constant with type C(1) and initial value SPACE. MOVE <fs> TO dest Transfers the initial value SPACE to the variable dest MOVE 'A' to <fs> Not possible, since <fs> is a constant (runtime error). To lift a type restriction, use the CASTING addition in the ASSIGN statement. 

The data object is then interpreted as though it had the data type of the field symbol. You can also do this with untyped field symbols using the CASTING TYPE <type> addition. The danger with pointers is that they may point to invalid areas. This danger is not so acute in ABAP, because the language does not use address arithmetic (for example, in other languages, pointer p might point to address 1024. After the statement p = p + 10, it would point to the address 1034). However, the danger does still exist, and memory protection violations lead to runtime errors. A pointer in ABAP may not point beyond a segment boundary. ABAP does not have one large address space, but rather a set of segments. Each of the following has its own segment: * All global data * All local data * Each table work area (TABLES) * Each COMMON PART You should only let field symbols move within an elementary field or structure where ABAP allows you to assign both within the global data and beyond a field boundary.


You can find more information about Field Symbols by attending BC402:ABAP Programming Techiquee of courses on the ABAP programming language.
Check out our website at http://www.sap.com/usa/education for course outlines and schedules.

This was first published in January 2002
This Content Component encountered an error

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close