SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Declarations → Typing →Generic and Complete Typing
The example shows how to access generically and completely typed formal parameters.
Source Code
REPORT demo_typing.
CLASS demo_typing DEFINITION.
PUBLIC SECTION.
CLASS-METHODS start.
PRIVATE SECTION.
TYPES: BEGIN OF struc1,
cola TYPE i,
colb TYPE i,
END OF struc1,
BEGIN OF struc2,
colb TYPE i,
cola TYPE i,
END OF struc2,
itab1g TYPE TABLE OF struc1,
itab2g TYPE TABLE OF struc2,
itab2c TYPE TABLE OF struc2 WITH NON-UNIQUE DEFAULT KEY.
CLASS-METHODS sort_itab IMPORTING VALUE(pg) TYPE itab2g
VALUE(pc) TYPE itab2c.
ENDCLASS.
CLASS demo_typing IMPLEMENTATION.
METHOD start.
DATA: tab TYPE itab1g,
wa LIKE LINE OF tab.
DO 5 TIMES.
wa-cola = sy-index.
wa-colb = 6 - sy-index.
APPEND wa TO tab.
ENDDO.
sort_itab( pg = tab
pc = tab ).
ENDMETHOD.
METHOD sort_itab.
DATA(out) = cl_demo_output=>new( ).
SORT pg BY cola.
out->write_data( pg ).
SORT pg BY ('COLA').
out->write_data( pg ).
SORT pc BY cola.
out->write_data( pc ).
SORT pc BY ('COLA').
out->write_data( pc ).
out->display( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo_typing=>start( ).
Description
In the class demo_typing, three table types are declared:
The method sort_itab has two formal parameters:
In the method start, an internal table tab is declared by referring to itab1g. The key is specified implicitly. This table is filled and passed to the formal parameters of sort_itab.
In sort_itab, both formal parameters are sorted twice. In the first sort, the column cola is specified statically as a sort criterion, in the second sort, it is specified dynamically. The behaviour is as follows:
If components of generically typed formal parameters are accessed dynamically, a runtime error may occur if the components do not exist in the actual parameter.