Pass dynamic table as reference out of method
source link: https://blogs.sap.com/2022/09/19/pass-dynamic-table-as-reference-out-of-method/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Pass dynamic table as reference out of method
Ever wondered how to get out your inline created dynamic table out of your method / sub procedure?
As known, only references can be passed through a method interface. But I failed always, because the referred table was empty outside of the method (freed stack) because it is created on stack memory which is only valid inside of the method scope.
So I managed to dynamically create a table of the same structure using RTTS and copied the content of the table in there. This table can be assigned to a class attribute reference without loosing the content. Why is ABAP that laborious?
CLASS lc_flights DEFINITION. PUBLIC SECTION. METHODS get_data. METHODS display_alv. PRIVATE SECTION. DATA _r_datatable TYPE REF TO data. ENDCLASS. CLASS lc_flights IMPLEMENTATION. METHOD get_data. * create a fancy dynamic table SELECT FROM sflights AS f JOIN scarr AS c ON c~carrid = f~carrid FIELDS c~*, f~* INTO TABLE @DATA(lt_flight). *** copy table to heap *** * Create table description DATA(lo_tabledesc) = CAST cl_abap_tabledescr( cl_abap_tabledescr=>describe_by_data( p_data = lt_flight ) ). * Create table in heap memory and use a reference from outside the method CREATE DATA _r_datatable TYPE HANDLE lo_tabledesc. FIELD-SYMBOLS <lt_datatable> LIKE lt_flight. " Create a field-symbol because append doesn't work with references ASSIGN _r_datatable->* TO <lt_datatable>. " APPEND LINES OF lt_flight TO <lt_datatable>. " Copy content to heap table ************************** ENDMETHOD. METHOD display_alv. FIELD-SYMBOLS <lt_datatable> TYPE ANY TABLE. ASSIGN _r_datatable->* TO <lt_datatable>. cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lo_salv) CHANGING t_table = <lt_datatable> ). lo_salv->display( ). ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA(lo_flights) = NEW lc_flights( ). lo_flights->get_data( ). lo_flights->display_alv( ).
Is there any more simple solution for that? Please let me know in the comments.
Please follow my profile.
Read also:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK