Skip to content

Commit 7620263

Browse files
committed
Update
1 parent 18f4dbe commit 7620263

11 files changed

+711
-128
lines changed

02_Structures.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [Creating Anonymous Structures](#creating-anonymous-structures)
1616
- [Variants of Structures](#variants-of-structures)
1717
- [Accessing (Components of) Structures](#accessing-components-of-structures)
18+
- [ASSIGN Statements](#assign-statements)
1819
- [Populating Structures](#populating-structures)
1920
- [Using the VALUE Operator](#using-the-value-operator)
2021
- [Using the NEW Operator](#using-the-new-operator)
@@ -462,6 +463,102 @@ Nested components can be addressed using chaining:
462463
463464
<p align="right"><a href="#top">⬆️ back to top</a></p>
464465

466+
### ASSIGN Statements
467+
468+
```abap
469+
"A field symbol is set using an assignment of a memory area to the
470+
"field symbol by ASSIGN statements.
471+
"Particularly, field symbols and ASSIGN statements are supporting elements for
472+
"dynamic programming. ASSIGN statements have multiple additions. Find more information
473+
"and examples in the Dynamic Programming cheat sheet.
474+
475+
TYPES: BEGIN OF s,
476+
comp1 TYPE i,
477+
comp2 TYPE c LENGTH 3,
478+
comp3 TYPE n LENGTH 5,
479+
comp4 TYPE string,
480+
END OF s.
481+
482+
DATA(demo_struc) = VALUE s( comp1 = 1 comp2 = 'abc' comp3 = '12345' comp4 = `ABAP` ).
483+
484+
"Defining a field symbol
485+
FIELD-SYMBOLS <a> TYPE s.
486+
487+
"Assigning the entire structure to a field symbole
488+
ASSIGN demo_struc TO <a>.
489+
490+
"Accessing a structure component via the field symbol
491+
DATA(comp1) = <a>-comp1.
492+
493+
"Field symbol declared inline
494+
"Note: The typing depends on the memory area specified. In this case,
495+
"the field symbol <fd> has the structured type s. This is valid for static
496+
"assignments. In case of dynamic assignments, the type is the generic type
497+
"data.
498+
ASSIGN demo_struc TO FIELD-SYMBOL(<b>).
499+
comp1 = <b>-comp1.
500+
501+
"Accessing components of structures by assigning components to field symbols
502+
"The field symbols is typed with the generic type data so that all components,
503+
"which have random types, can be assigned.
504+
FIELD-SYMBOLS <d> TYPE data.
505+
ASSIGN demo_struc-comp1 TO <d>.
506+
ASSIGN demo_struc-comp2 TO <d>.
507+
ASSIGN demo_struc-comp3 TO <d>.
508+
ASSIGN demo_struc-comp4 TO <d>.
509+
510+
"Accessing structures and components dynamically
511+
"Note: In case of dynamic assignments, ...
512+
"- sy-subrc is set.
513+
"- the type of field symbols declared inline is the generic type data.
514+
ASSIGN ('DEMO_STRUC') TO FIELD-SYMBOL(<c>).
515+
ASSERT sy-subrc = 0.
516+
517+
"Using ASSIGN COMPONENT statements
518+
"It is recommended to use the newer syntax, which sepcifies the component
519+
"selector followed by a data object in a pair of parentheses.
520+
"After COMPONENT (and in the parentheses in the newer syntax), a character-like
521+
"or numeric data object is expected.
522+
DATA(some_comp) = `COMP2`.
523+
ASSIGN COMPONENT some_comp OF STRUCTURE demo_struc TO <d>.
524+
ASSERT sy-subrc = 0.
525+
526+
some_comp = `COMP5`.
527+
ASSIGN COMPONENT some_comp OF STRUCTURE demo_struc TO <d>.
528+
ASSERT sy-subrc = 4.
529+
530+
"Numeric data objects
531+
"The data object is implicitly converted to type i (if required) and
532+
"interpreted as the position of the component in the structure.
533+
ASSIGN COMPONENT 1 OF STRUCTURE demo_struc TO <d>.
534+
ASSERT sy-subrc = 0.
535+
536+
ASSIGN COMPONENT 5 OF STRUCTURE demo_struc TO <d>.
537+
ASSERT sy-subrc = 4.
538+
539+
"0 means that the entire structure is assigned
540+
ASSIGN COMPONENT 0 OF STRUCTURE demo_struc TO <d>.
541+
ASSERT sy-subrc = 0.
542+
ASSERT <d> = demo_struc.
543+
544+
"Newer syntax using the component selector followed by content in
545+
"a pair of parentheses.
546+
ASSIGN demo_struc-('COMP4') TO <d>.
547+
ASSIGN demo_struc-(some_comp) TO <d>.
548+
ASSIGN demo_struc-(3) TO <d>.
549+
ASSIGN demo_struc-(0) TO <d>.
550+
551+
"Iterating across all structure components
552+
DO.
553+
ASSIGN demo_struc-(sy-index) TO <d>.
554+
IF sy-subrc <> 0.
555+
EXIT.
556+
ENDIF.
557+
ENDDO.
558+
```
559+
560+
<p align="right"><a href="#top">⬆️ back to top</a></p>
561+
465562
## Populating Structures
466563

467564
You can copy the content of a structure to another using the [assignment operator](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenassignment_operator_glosry.htm) `=`.
@@ -1090,7 +1187,7 @@ ENDLOOP.
10901187
- More information about the purpose of the individual components is available at [ABAP System Fields (F1 documentation for Standard ABAP)](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abensystem_fields.htm).
10911188

10921189

1093-
The following example demonstrates a selection of ABAP system fields. It uses artifacts from the ABAP cheat sheet repository. Note the comments in the code because a syntax warning will be displayed when inserting the code in a demo class that uses ABAP for Cloud Development. It is meant to emphasize that multiple system fields should not to be used in ABAP for Cloud Development.
1190+
The following example demonstrates a selection of ABAP system fields. It uses artifacts from the ABAP cheat sheet repository. Note the comments in the code because a syntax warning will be displayed when inserting the code in a demo class that uses ABAP for Cloud Development. It is meant to emphasize that multiple system fields should not be used in ABAP for Cloud Development.
10941191
To try the example out, create a demo class named `zcl_demo_abap` and paste the code into it. After activation, choose *F9* in ADT to execute the class. The example is set up to display output in the console.
10951192

10961193
```abap

04_ABAP_Object_Orientation.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4371,7 +4371,7 @@ Global class
43714371
<td>
43724372

43734373
``` abap
4374-
CLASS zcl_some_class DEFINITION
4374+
CLASS zcl_demo_abap DEFINITION
43754375
PUBLIC
43764376
FINAL
43774377
CREATE PUBLIC .
@@ -4383,7 +4383,7 @@ CLASS zcl_some_class DEFINITION
43834383
PRIVATE SECTION.
43844384
ENDCLASS.
43854385
4386-
CLASS zcl_some_class IMPLEMENTATION.
4386+
CLASS zcl_demo_abap IMPLEMENTATION.
43874387
METHOD if_oo_adt_classrun~main.
43884388
43894389
"Object reference variables
@@ -4629,7 +4629,7 @@ Global class
46294629
<td>
46304630

46314631
``` abap
4632-
CLASS zcl_some_class DEFINITION
4632+
CLASS zcl_demo_abap DEFINITION
46334633
PUBLIC
46344634
FINAL
46354635
CREATE PUBLIC .
@@ -4641,7 +4641,7 @@ CLASS zcl_some_class DEFINITION
46414641
PRIVATE SECTION.
46424642
ENDCLASS.
46434643
4644-
CLASS zcl_some_class IMPLEMENTATION.
4644+
CLASS zcl_demo_abap IMPLEMENTATION.
46454645
METHOD if_oo_adt_classrun~main.
46464646
46474647
"Object reference variables

06_Dynamic_Programming.md

Lines changed: 151 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ FIELD-SYMBOLS:
319319
<simple> TYPE simple, "Elementary data type including enumerated types and
320320
"structured types with exclusively character-like flat components
321321
<object> TYPE REF TO object. "object can only be specified after REF TO; can point to any object
322+
"The types REF TO object and REF TO data are considered complete types.
322323
323324
"Data objects to work with
324325
DATA: BEGIN OF s,
@@ -428,6 +429,7 @@ ASSIGN s-structure TO <simple>.
428429
ASSIGN s-xl1 TO <simple>.
429430
"ASSIGN s-tab_ha TO <simple>.
430431
432+
s-oref = NEW cl_system_uuid( ).
431433
ASSIGN s-oref TO <object>.
432434
```
433435

@@ -459,15 +461,20 @@ ASSIGN s-oref TO <object>.
459461
#### Declaring Data Reference Variables
460462

461463
``` abap
462-
"Example declarations of data reference variables with static types.
463-
"The static types can be complete or generic (but only data can be used).
464-
"Note that they do not yet point to a data object. At this stage,
465-
"initial reference variables contain null references.
464+
"The following example shows a selection of declaration options with data reference
465+
"variables with static types. The static types can be complete or generic (but
466+
"only data can be used).
467+
"Note that the variables do not yet point to a data object. At this stage,
468+
"initial reference variables contain null references.
469+
DATA num type i.
470+
466471
DATA: ref_a TYPE REF TO i, "Complete data type
467-
ref_b TYPE REF TO some_dbtab, "Complete data type
468-
ref_c LIKE REF TO some_data_object,
472+
ref_b TYPE REF TO zdemo_abap_carr, "Complete data type
473+
ref_c LIKE REF TO num,
469474
ref_d TYPE REF TO data, "Generic data type
470-
ref_e LIKE ref_a. "Referring to an existing data reference variable
475+
ref_e LIKE ref_a, "Referring to an existing data reference variable
476+
ref_f TYPE TABLE OF REF TO i, "Reference table, complete data type
477+
ref_g TYPE TABLE OF REF TO data. "Reference table, generic data type
471478
```
472479

473480
As shown below, instead of the explicit declaration, inline declarations are also possible.
@@ -1047,6 +1054,7 @@ DATA(idx) = line_index( dref->*[ ('CARRID') = 'LH' ] ).
10471054
Some example contexts of using data references are as follows:
10481055

10491056
*Overwriting data reference variables*:
1057+
10501058
``` abap
10511059
DATA dref TYPE REF TO data.
10521060
dref = NEW i( 1 ).
@@ -1108,6 +1116,7 @@ READ TABLE fli_tab INDEX 1 REFERENCE INTO DATA(rt_ref).
11081116
```
11091117

11101118
*Data reference variables as part of structures and internal tables*:
1119+
11111120
``` abap
11121121
"Unlike field symbols, data reference variables can be used as
11131122
"components of structures or columns in internal tables.
@@ -1129,6 +1138,19 @@ APPEND struc TO itab.
11291138
itab[ 1 ]-ref->* = 123.
11301139
```
11311140

1141+
*Internal tables of type REF TO data can contain any data references*:
1142+
1143+
```abap
1144+
DATA it_ref TYPE TABLE OF REF TO data.
1145+
1146+
"Such a table can hold any data type
1147+
it_ref = VALUE #( ( NEW i( 3 ) ) "Elementary type
1148+
( NEW string( `hello` ) ) "Elementary type
1149+
( NEW zdemo_abap_flsch( carrid = 'XY' connid = '1234' ) ) "Structured type
1150+
( NEW string_table( ( `a` ) ( `b` ) ( `c` ) ) ) "Table type
1151+
).
1152+
```
1153+
11321154
> **✔️ Hint**<br>
11331155
> When to actually use either a field symbol
11341156
or a data reference variable? It depends on your use case. However, data
@@ -2309,8 +2331,7 @@ ENDCLASS.
23092331

23102332
### Dynamic Method Calls
23112333

2312-
2313-
- The following code snippet shows dynamically specifying method calls.
2334+
- The following code snippet shows dynamically specifying method calls. Find three example classes below the syntax pattern snippet.
23142335
- The snippet covers methods. Dynamic method calls require `CALL METHOD` statements.
23152336
- Find an example of a dynamic function module call - as another example of [procedure](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenprocedure_glosry.htm "Glossary Entry") calls - in the [Program Flow Logic](./13_Program_Flow_Logic.md#function-module-example) cheat sheet.
23162337

@@ -2359,7 +2380,14 @@ CALL METHOD class=>(meth) PARAMETER-TABLE ptab.
23592380
"The addition EXCEPTION-TABLE for exceptions is not dealt with here.
23602381
```
23612382

2362-
**Example class 1**
2383+
2384+
**Click to expand the expandable sections for example code**
2385+
2386+
<details>
2387+
<summary>🟢 Example class 1</summary>
2388+
<!-- -->
2389+
2390+
<br>
23632391

23642392
The following example class explores dynamic method calls with simple methods. You can create a demo class called `zcl_demo_abap` and copy and paste the following code. Once activated, you can choose *F9* in ADT to run the class. The example is not designed to display output in the console.
23652393

@@ -2547,8 +2575,16 @@ CLASS zcl_demo_abap IMPLEMENTATION.
25472575
ENDCLASS.
25482576
```
25492577

2578+
</details>
2579+
2580+
<br>
2581+
2582+
<details>
2583+
<summary>🟢 Example class 2</summary>
2584+
<!-- -->
2585+
2586+
<br>
25502587

2551-
**Example class 2**
25522588

25532589
The following simplified example highlights several things in the context of a dynamic method call example:
25542590
- Dynamic method call and assigning actual parameters to formal parameters statically
@@ -2624,6 +2660,110 @@ CLASS zcl_demo_test IMPLEMENTATION.
26242660
ENDCLASS.
26252661
```
26262662

2663+
</details>
2664+
2665+
<br>
2666+
2667+
<details>
2668+
<summary>🟢 Example class 3</summary>
2669+
<!-- -->
2670+
2671+
<br>
2672+
2673+
The following example demonstrates dynamic method calls with methods defined in interfaces.
2674+
2675+
<table>
2676+
2677+
<tr>
2678+
<td> Class include </td> <td> Code </td>
2679+
</tr>
2680+
2681+
<tr>
2682+
<td>
2683+
2684+
Global class
2685+
2686+
</td>
2687+
2688+
<td>
2689+
2690+
``` abap
2691+
CLASS zcl_demo_abap DEFINITION
2692+
PUBLIC
2693+
FINAL
2694+
CREATE PUBLIC .
2695+
2696+
PUBLIC SECTION.
2697+
INTERFACES if_oo_adt_classrun.
2698+
PROTECTED SECTION.
2699+
PRIVATE SECTION.
2700+
ENDCLASS.
2701+
2702+
CLASS zcl_demo_abap IMPLEMENTATION.
2703+
METHOD if_oo_adt_classrun~main.
2704+
2705+
"Creating an instance of the local class that implements
2706+
"an interface
2707+
DATA(oref) = NEW lcl( ).
2708+
2709+
"Static method call
2710+
DATA(number) = oref->lif~get_number( ).
2711+
out->write( number ).
2712+
2713+
"Dynamic method call
2714+
"Note the uppercase.
2715+
TRY.
2716+
CALL METHOD oref->('lif~get_number') RECEIVING number = number.
2717+
out->write( number ).
2718+
CATCH cx_sy_dyn_call_illegal_method INTO DATA(error).
2719+
DATA(error_text) = error->get_text( ).
2720+
out->write( error_text ).
2721+
ENDTRY.
2722+
2723+
CALL METHOD oref->('LIF~GET_NUMBER') RECEIVING number = number.
2724+
out->write( number ).
2725+
ENDMETHOD.
2726+
ENDCLASS.
2727+
```
2728+
2729+
</td>
2730+
</tr>
2731+
2732+
<tr>
2733+
<td>
2734+
2735+
CCIMP include (*Local Types* tab in ADT)
2736+
2737+
</td>
2738+
2739+
<td>
2740+
2741+
``` abap
2742+
INTERFACE lif.
2743+
METHODS get_number RETURNING VALUE(number) TYPE i.
2744+
ENDINTERFACE.
2745+
2746+
CLASS lcl DEFINITION.
2747+
PUBLIC SECTION.
2748+
INTERFACES lif.
2749+
ENDCLASS.
2750+
2751+
CLASS lcl IMPLEMENTATION.
2752+
METHOD lif~get_number.
2753+
number = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
2754+
min = 1
2755+
max = 1000 )->get_next( ).
2756+
ENDMETHOD.
2757+
ENDCLASS.
2758+
```
2759+
2760+
</td>
2761+
</tr>
2762+
2763+
</table>
2764+
</details>
2765+
2766+
26272767
<p align="right"><a href="#top">⬆️ back to top</a></p>
26282768

26292769
### Dynamic Function Module Calls

0 commit comments

Comments
 (0)