Field Symbols New

download Field Symbols New

of 6

Transcript of Field Symbols New

  • 8/13/2019 Field Symbols New

    1/6

    Field Symbols

    Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for afield, but point to its contents. A field symbol cam point to any data object. The data object to which a fieldsymbol points is assigned to it after it has been declared in the program.

    Whenever you address a field symbol in a program, you are addressing the field that is assigned to the fieldsymbol. After successful assignment, there is no difference in ABAP whether you reference the field symbol or

    the field itself. You must assign a field to a field symbol before you can address it in a program.

    All operations programmed with field symbols are applied to the field assigned to it. For example, a MOVEstatement between two field symbols moves the contents of the field assigned to the first field symbol to the fieldassigned to the second field symbol. The field symbols themselves point to the same fields after the MOVEstatement as they did before.

    You can create field symbols either without or with type specifications. If you do not specify a type, the fieldsymbol inherits all of the technical attributes of the field assigned to it. If you do specify a type, the systemchecks the compatibility of the field symbol and the field you are assigning to it during the ASSIGN statement.

    Field symbols provide greater flexibility when you address data objects:

    o If you want to process sections of fields, you can specify the offset and length of the field dynamically.o You can assign one field symbol to another, which allows you to address parts of fields.o Assignments to field symbols may extend beyond field boundaries. This allows you to address regular

    sequences of fields in memory efficiently.o You can also force a field symbol to take different technical attributes from those of the field assigned to

    it.

    The flexibility of field symbols provides elegant solutions to certain problems. On the other hand, it does meanthat errors can easily occur. Since fields are not assigned to field symbols until runtime, the effectiveness ofsyntax and security checks is very limited for operations involving field symbols. This can lead to runtime errorsor incorrect data assignments.

    While runtime errors indicate an obvious problem, incorrect data assignments are dangerous because they canbe very difficult to detect. For this reason, you should only use field symbols if you cannot achieve the sameresult using other ABAP statements.

    For example, you may want to process part of a string where the offset and length depend on the contents of thefield. You could use field symbols in this case. However, since the MOVE statement also supports variable offsetand length specifications, you should use it instead. The MOVE statement (with your own auxiliary variables ifrequired) is much safer than using field symbols, since it cannot address memory beyond the boundary of a field.However, field symbols may improve performance in some cases.

    Defining Field Symbols

    Defining Field SymbolsTo declare a field symbol, use the statement

    FIELD-SYMBOLS [|STRUCTURE DEFAULT ].

  • 8/13/2019 Field Symbols New

    2/6

    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 can have data objects of any time assigned to it. Whenyou assign a data object, the field symbol inherits its technical attributes. The data type of the assigned dataobject becomes the actual data type of the field symbol.

    Note that although it possible to assign reference variables and structured data objects to untypedfield symbols,the static field symbol is only a pointer to the field in memory, and does not have the complex type attributes of areference 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 or LOOP AT arenot possible.

    Typing Field Symbols

    The addition allows you to specify the type of a field symbol. When you assign a data object to a fieldsymbol, the system checks whether the type of the data object you are trying to assign is compatible with that ofthe field symbol. If the types are not compatible or convertible, the system reacts with a syntax or runtime error.You can only assign variables to a field symbol if their type is compatible with that of the field symbol (seeDefining the Type of a Field Symbol). In this case, the field symbol retains its original type, regardless of the typeof the data object assigned to it.

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

    Generic Type Specification

    The following types allow you more freedom when using actual parameters. The data object only needs to havethe selection of attributes specified.

    Type specification Check for data object

    No type specification

    TYPE ANY

    All types of data object are accepted. The field symbol adopts all of theattributes of the data object.

    TYPE C, N, P, or X Only data objects with type C, N, P, or X are accepted. The field symboladopts the field length and DECIMALS specification (type P) of the dataobject.

    TYPE TABLE The system checks whether the data object is a standard internal table.This is a shortened form of TYPE STANDARD TABLE (see below).

    TYPE ANY TABLE The system checks whether the data object is an internal table. The fieldsymbol inherits all of the attributes (line type, table type, key) from thedata object.

    TYPE INDEX TABLE The system checks whether the data object is an index table (standard or

    sorted table). The field symbol inherits all of the attributes (line type, tabletype, key) from the data object.

    TYPE STANDARD TABLE The system checks whether the data object is a standard internal table.The field symbol inherits all of the remaining attributes (line type, key)from the data object.

    TYPE SORTED TABLE The system checks whether the actual parameter is a sorted internaltable. The field symbol inherits all of the remaining attributes (line type,key) from the data object.

    TYPE HASHED TABLE The system checks whether the actual parameter is a hashed internal

  • 8/13/2019 Field Symbols New

    3/6

    table. The field symbol inherits all of the remaining attributes (line type,key) from the data object.

    If you specify a type generically, remember that the attributes inherited by the field symbol from the program arenot statically recognizable in the program. You can, at most, address them dynamically.

    TYPES: BEGIN OF LINE,

    COL1,

    COL2,

    END OF LINE.

    DATA: WA TYPE LINE,

    ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1,

    KEY(4) VALUE 'COL1'.

    FIELD-SYMBOLS TYPE ANY TABLE.

    ASSIGN ITAB TO .

    READ TABLE WITH TABLE KEY (KEY) = 'X' INTO WA.

    The internal table ITAB is assigned to the generic field symbol , after which it is possible toaddress the table key of the field symbol dynamically. However, the static address

    READ TABLE WITH TABLE KEY COL1 = 'X' INTO WA.

    is syntactically not possible, since the formal parameter P does not adopt the key of table ITABuntil runtime. In the program, the type specification ANY TABLE only indicates that is atable. If the type had been ANY (or no type had been specified at all), even the specific internaltable statement READ TABLE would not have been possible.

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

    Specifying the Type Fully

    When you use the following types, the technical attributes of the field symbols are fully specified. The technicalattributes of the data objects must correspond to those of the field symbol.

    Type specification Technical attributes of the field symbol

    TYPE D, F, I, or T The field symbol has the technical attributes of the predefined elementarytype

    TYPE The field symbol has the type . This is a data type defined withinthe program using the TYPES statement, or a type from the ABAPDictionary

    TYPE REF TO The field symbol is a reference variable (ABAP Objects) for the class orinterface

    TYPE LINE OF The field symbol has the same type as a line of the internal table defined using a TYPES statement or defined in the ABAP Dictionary

    LIKE The field symbol has the same type as an internal data object orstructure, or a database table from the ABAP Dictionary

    When you use a field symbol that is fully typed, you can address its attributes statically in the program, sincethey are recognized in the source code. If you fully specify the type of a field symbol as a reference or structured

  • 8/13/2019 Field Symbols New

    4/6

    data object, you can address it as you would the data object itself once you have assigned an object to it. So, forexample, you could address the components of a structure, loop through an internal table, or create an objectwith reference to a field symbol.

    DATA: BEGIN OF LINE,

    COL1,

    COL2 VALUE 'X',

    END OF LINE.

    FIELD-SYMBOLS LIKE LINE.

    ASSIGN LINE TO .

    MOVE -COL2 TO -COL1.

    The field symbol is fully typed as a structure, and you can address its components in theprogram.

    Attaching a Structure to a Field Symbol

    The STRUCTURE addition forces a structured viewof the data objects that you assign to a field symbol.

    FIELD-SYMBOLS STRUCTURE DEFAULT .

    The structure is either a structured local data object in the program, or a flat structure from the ABAPDictionary. is a data object that must be assigned to the field symbol as a starting field. However, thisassignment can be changed later using the ASSIGN statement.

    When you assign a data object to the field symbol, the system only checks whether it is at least as long as thestructure. You can address the individual components of the field symbol. It has the same technical attributes asthe structure .

    If contains components with type I or F, you should remember the possible effects of alignment. When you

    assign a data object to a field symbol with a structure, the data object must have the same alignment, otherwisea runtime error may result. In such cases, you are recommended to assign such data objects only to structuredfield symbols which retain the same structure as the field symbol at least over the length of the structure.

    DATA: WA(10) VALUE '0123456789'.

    DATA: BEGIN OF LINE1,

    COL1(3),

    COL2(2),

    COL3(5),

    END OF LINE1.

    DATA: BEGIN OF LINE2,

    COL1(2),

    COL2 LIKE SY-DATUM,

    END OF LINE2.

    FIELD-SYMBOLS: STRUCTURE LINE1 DEFAULT WA,

    STRUCTURE LINE2 DEFAULT WA.

  • 8/13/2019 Field Symbols New

    5/6

    WRITE: / -COL1, -COL2, -COL3,

    / -COL1, -COL2.

    The output is:

    012 34 5678901 2345/67/89

    This example declares two field symbols to which different structures are attached. The string

    WA is then assigned to each of them. The output shows that the field symbols assign the stringscomponent by component according to the type of the component.

  • 8/13/2019 Field Symbols New

    6/6