1
votes

Is it possible to return an internal table when calling a method of a class?

The idea is the following: In my method, i am calculating all periods within a certain date-range. E.g my range is: 01.01.2020 - 31.03.2020, so I want to get an internal table with the following results:

01.01.2020 - 31.01.2020
01.02.2020 - 28.02.2020
01.03.2020 - 31.03.2021

The calculation already works and i can display the results via the WRITE statement, but I am not sure how to return the result. I created an internal table with the following structure:

TYPES: BEGIN OF periods, 
       begda TYPE dats,
       endda TYPE dats,
       END OF periods.
DATA: lt_periods TYPE STANDARD TABLE OF periods.

But I don't understand how to return the data to work with it in another method. Thank you in advance.

2

2 Answers

4
votes

Yes, it is possible to have an internal table as returning parameter of a method. The type of the returning parameter has to be a table type, so a table type has to be declared:

TYPES tt_periods TYPE STANDARD TABLE OF periods WITH DEFAULT KEY. "As pointed out by Sandra, see below :)

And the method is declared like this:

METHODS method
  ...               "IMPORTING parameters (if exist)
  RETURNING
    VALUE(rt_periods) TYPE tt_periods.
0
votes

Just to add a little bit, you can do as follow:

  • define your data type in the public / protected or private section of your class
  • return / export / change in your method the specific data type defined above

If you data type definition is done in public section this will become visible for any other ABAP objects (for example you can use it in a function module definition or in other ABAP objects, as: <class_name>=><type_name>)