Welcome to SAP Central
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Passing internal table as method parameter

Go down

Passing internal table as method parameter Empty Passing internal table as method parameter

Post  Uma_ABAP Sat Mar 31, 2012 11:39 pm


As we have seen in the earlier section that a method can import as well as export
parameters, but this is not all it can import as well as export structures internal table etc
like forms .In this section we will show how to pass an internal table as parameter to a
method.

A point to note the internal table that is to be passed to the method should not
have header line as this is against the concept of OOP. While defining the method it is
necessary to give the type of the internal table to be passed;

we can give the type as any predefined structure of the database dictionary. But it is convenient to keep the type
declaration more generic, because when we pass the actual internal table the whole internal
table is passed into the method, and the method takes the structure of the internal table
that has been actually passed .To keep the type declaration generic, we declare the type as any.

Code:

CLASS test DEFINITION.
PUBLIC SECTION.
METHODS: test_method_itab EXPORTING itab TYPE ANY TABLE.
ENDCLASS.

CLASS test IMPLEMENTATION.
METHOD test_method_itab.
**** wa is a work area it must be declared in the main program or it should be any
****dictionary structure.

Loop at itab into wa.
Write:/ wa-field_1,20 wa-field_2.
Endloop.
ENDMETHOD.
ENDCLASS.

Example
In this example the things that were covered in the earlier sections have been summarized.
This is a full working program which , shows how a method can import as well as export
parameters, it has been also show how internal table can be passed into a methods and how
to process the internal table in the method .

Code:

CLASS test DEFINITION.
PUBLIC SECTION.
METHODS: read_data IMPORTING num1 TYPE I num2 TYPE I RETURNING
value(result1) TYPE I ,
print_itab exporting itab1 type any table.
PRIVATE SECTION.
DATA:NUMP1 TYPE I,
NUMP2 LIKE NUMP1,
RESULT LIKE NUMP1.
ENDCLASS.

CLASS test IMPLEMENTATION.
METHOD read_data.
nump1 = num1.
nump2 = num2.
result1 = nump1 + nump2.
ENDMETHOD.
METHOD print_itab.
LOOP AT ITAB1 into Wa
WRITE:/5 wa-NAME,20 wa-ROLL,30 wa-HALL.
ENDLOOP.
Clear wa.
ENDMETHOD.
ENDCLASS.

**************************MAIN PROGRAM*****************************
TABLES:ystudent.
TYPES:BEGIN OF STRUCT,
NAME LIKE YSTUDENT-NAME,
ROLL LIKE YSTUDENT-ROLLNO,
HALL LIKE YSTUDENT-HALL,
END OF STRUCT.

DATA:ITAB1 TYPE STANDARD TABLE OF STRUCT INITIAL SIZE 0,
worka LIKE LINE OF ITAB1.
PARAMETERS:num1 TYPE I,
num2 TYPE I,
result TYPE I.
DATA:test TYPE REF TO test.
CREATE OBJECT test TYPE test.
START-OF-SELECTION.
WRITE:/5 num1,
/5 num2.

SELECT: NAME ROLLNO HALL FROM YSTUDENT INTO TABLE ITAB1 WHERE HALL = 'RP'.

result = test->read_data( num1 = num1 num2 = num2 ).
CALL METHOD test->print_itab importing itab1 = itab1.
ULINE.
WRITE:/5 result.
END-OF-SELECTION.

Uma_ABAP

Posts : 56
Join date : 2012-03-31

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum