a month ago
I have the below piece of code, I expect that the field symbol is not assigned for the component ATRIBUT (the component doesn't exist), but it says it's assigned, with the value of the previous component ID:
REPORT zdemo.
CLASS ltc_is_assigned_wrong_usage DEFINITION FINAL
FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PRIVATE SECTION.
METHODS test FOR TESTING RAISING cx_static_check.
ENDCLASS.
CLASS ltc_is_assigned_wrong_usage IMPLEMENTATION.
METHOD test.
TYPES:
BEGIN OF ty_object,
id TYPE c LENGTH 10,
attribute TYPE c LENGTH 10,
END OF ty_object.
DATA(object) = VALUE ty_object( id = 'Value-ID'
attribute = 'Value-Attr' ).
DATA(component_names) = VALUE string_table( ( `WRONG` ) ( `ID` ) ( `ATRIBUT` ) ( `ATTRIBUTE` ) ).
DATA(result) = VALUE string( ).
LOOP AT component_names INTO DATA(component_name).
ASSIGN COMPONENT component_name OF STRUCTURE object TO FIELD-SYMBOL(<component>).
IF <component> IS ASSIGNED. "<===== doesn't work well ???
result = result && |{ component_name } value: { <component> }\n|.
ELSE.
result = result && |{ component_name } not assigned\n|.
ENDIF.
ENDLOOP.
cl_abap_unit_assert=>assert_equals(
act = result
exp = |WRONG not assigned\n|
&& |ID value: Value-ID\n|
&& |ATRIBUT value: Value-ID\n| "<==== I expect ATRIBUT not assigned
&& |ATTRIBUTE value: Value-Attr\n| ).
ENDMETHOD.
ENDCLASS.
Why does "IS ASSIGNED" return true for the non-existing component name ATRIBUT?
Thank you.
Sandra
Request clarification before answering.
Be very careful. ASSIGN ... TO <FS> does not unassign the field symbol <FS> if ASSIGN fails, <FS> keeps its previous assignment.
If you use or see code containing "IS ASSIGNED". As a rule of thumb, the result will could be wrong (EDIT: I mean that there should be a kind of spiderman alert, not that it's wrong, but it could be wrong, so it should be double checked, especially inside a loop).
IS ASSIGNED can work only if the field symbol is assigned only once in its scope (i.e. a local field symbol assigned once in its method, function module or subroutine, a global field symbol assigned once when running a program or function group...) If it's needed to check the result of ASSIGN later, use "ASSIGN ... IF sy-subrc <> 0 .... UNASSIGN <fs>" (or ASSIGN ELSE UNASSIGN (*)) and you may then later use "IF <fs> IS ASSIGNED".
To avoid any issue, always use "IF sy-subrc = 0" after a Dynamic ASSIGN, i.e. after any ASSIGN which modifies SY-SUBRC:
Only few ASSIGN don't change SY-SUBRC. Often it can be only successful, in that case no need to test that the field symbol is assigned. Rarely it can fail, like ASSIGN <fs>+10 if <fs> length is less than 10, in that case the field symbol is unassigned implicitly. All these static forms are rarely used. When they are used, it can be the case to refer to one variable among few others of the same type (e.g. IF condition then ASSIGN var1 TO <fs> else ASSIGN var2 TO <fs>).
(*) EDIT: ASSIGN ... ELSE UNASSIGN exists since ABAP 7.57.
EDIT: could anyone find an example for the SAP statement "If a static assignment is not successful"? Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The statement "If you use or see code containing "IS ASSIGNED". As a rule of thumb, the result will be wrong." is misleading. The predicate IS ASSIGNED checks whether the field symbol is assigned. Nothing more and nothing less. Its result is never wrong. It can be used before addressing a field symbol in order to prevent exceptions. But nobody claims that it checks the last dynamic ASSIGN statement. If you want to check that with IS ASSIGNED, you must use the statement UNASSIGN directly before a dynamic ASSIGN, or more convenient the new addition ELSE UNASSIGN with dynamic ASSIGN. Static ASSIGN always works as with ELSE UNASSIGN. There it is OK to use IS ASSIGNED to check the assignment. I guess, you wanted to point out that it can be a trap if IS ASSIGNED is used wrongly with dynamic ASSIGN.
User | Count |
---|---|
34 | |
22 | |
16 | |
8 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.