You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"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
+
<palign="right"><ahref="#top">⬆️ back to top</a></p>
561
+
465
562
## Populating Structures
466
563
467
564
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.
1090
1187
- 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).
1091
1188
1092
1189
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.
1094
1191
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.
- 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.
"The addition EXCEPTION-TABLE for exceptions is not dealt with here.
2360
2381
```
2361
2382
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>
2363
2391
2364
2392
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.
2365
2393
@@ -2547,8 +2575,16 @@ CLASS zcl_demo_abap IMPLEMENTATION.
2547
2575
ENDCLASS.
2548
2576
```
2549
2577
2578
+
</details>
2579
+
2580
+
<br>
2581
+
2582
+
<details>
2583
+
<summary>🟢 Example class 2</summary>
2584
+
<!---->
2585
+
2586
+
<br>
2550
2587
2551
-
**Example class 2**
2552
2588
2553
2589
The following simplified example highlights several things in the context of a dynamic method call example:
2554
2590
- Dynamic method call and assigning actual parameters to formal parameters statically
@@ -2624,6 +2660,110 @@ CLASS zcl_demo_test IMPLEMENTATION.
2624
2660
ENDCLASS.
2625
2661
```
2626
2662
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
+
2627
2767
<palign="right"><ahref="#top">⬆️ back to top</a></p>
0 commit comments