Passing internal table as method parameter
Page 1 of 1
Passing internal table as method parameter
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
Similar topics
» What are internal tables? How do you get the number of lines in an internal table?
» What is occurs in internal table?
» Hashed Internal Table in SAP
» When an internal table is created, the settings criteria for the value of occurs?
» Method Redefinition in ABAP Objects
» What is occurs in internal table?
» Hashed Internal Table in SAP
» When an internal table is created, the settings criteria for the value of occurs?
» Method Redefinition in ABAP Objects
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Mon Aug 20, 2012 4:37 pm by Admin
» Difference between Type and Like
Thu Jun 07, 2012 2:40 pm by Admin
» How to handle table control fields in Module Pool program?
Wed May 09, 2012 2:41 am by govind
» Three tables inner join in SAP
Thu May 03, 2012 4:47 pm by Uma_ABAP
» Creating table type in ABAP
Sat Apr 28, 2012 9:03 pm by Uma_ABAP
» Creating user defined types and structures in ABAP
Sat Apr 28, 2012 8:55 pm by Uma_ABAP
» Creating Dynamic Internal Table and Dynamic Field catalog in SAP-ABAP
Fri Apr 27, 2012 11:00 am by Admin
» Copy Custom PF-Status from Standard program to Custom Program in ALV
Thu Apr 26, 2012 9:52 pm by Uma_ABAP
» Calculate/find number of working days between two dates in SAP
Tue Apr 24, 2012 2:06 pm by Uma_ABAP