05-12-2006 1:43 PM
Hi all,
How can we pass an internal table to a method of a class?
Pls Help.
Thanks in advance.
05-12-2006 1:47 PM
Hi anandaraja,
1. In classes (OO Concept),
there is no direct tables concept,
like the one we use in
FORM/PERFORM and FM.
2. Instead there is EXPORTING concept only.
3. The parameter is of type XYZ,
where XYZ is TABLE TYPE (in se11)
4. we have to define our variable like this .
data : itab type XYZ.
data : wa type line of XYZ.
(we use wa as a work area to put data
in itab using - append wa to itab.)
(we cannot define : data : itab like table of t001 with header line
It will give error)
5. then we pass the internal table (ITAB)
using EXPORT only. (not TABLES)
regards,
amit .
05-12-2006 1:47 PM
Hai AnandaRaja
Check the following Code
TABLES:MARA.
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.
CLASS C1 DEFINITION.
PUBLIC SECTION.
TYPES:BEGIN OF T_MARA,
MATNR TYPE MARA-MATNR,
AENAM TYPE MARA-AENAM,
END OF T_MARA.
DATA:ITAB TYPE STANDARD TABLE OF T_MARA,
WA_ITAB TYPE T_MARA.
EVENTS: E1 .
METHODS:GET_DATA,DISPLAY_DATA,NO_DATA FOR EVENT E1 OF C1..
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD: GET_DATA.
SELECT MATNR AENAM FROM MARA INTO TABLE ITAB WHERE MATNR IN S_MATNR.
IF SY-SUBRC <> 0.
RAISE EVENT E1.
ELSE .
CALL METHOD DISPLAY_DATA.
ENDIF.
ENDMETHOD.
METHOD:DISPLAY_DATA.
LOOP AT ITAB INTO WA_ITAB.
WRITE:/10 WA_ITAB-MATNR.
WRITE:40 WA_ITAB-AENAM.
ENDLOOP.
ENDMETHOD.
METHOD NO_DATA.
WRITE:/10 'C1: NO DATA FOR SELECTION CRITERIA'.
ENDMETHOD.
ENDCLASS.
***********************
CLASS C2 DEFINITION .
PUBLIC SECTION.
METHODS:NO_DATA FOR EVENT E1 OF C1.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
METHOD NO_DATA.
WRITE:/10 ' C2:NO DATA FOR SELECTION CRITERIA'.
ENDMETHOD.
ENDCLASS.
DATA:OBJ1 TYPE REF TO C1,
OBJ3 TYPE REF TO C1.
DATA:OBJ2 TYPE REF TO C2.
START-OF-SELECTION.
CREATE OBJECT: OBJ1,OBJ3 ,OBJ2.
SET HANDLER OBJ1->NO_DATA FOR: OBJ1.
CALL METHOD OBJ1->GET_DATA.
SET HANDLER OBJ1->NO_DATA FOR: OBJ1 ACTIVATION SPACE.
SET HANDLER OBJ2->NO_DATA FOR OBJ1.
CALL METHOD OBJ1->GET_DATA.
Thanks & Regards
Sreenivasulu P
05-12-2006 1:48 PM
hi
u can't pass internal tables with header line
call method class->method
exporting
gt_table = itab[].
05-12-2006 1:55 PM
hii
Try this
----
CLASS lcl_app DEFINITION
----
........ *
----
<b>class lcl_app definition.
public section.
types: t_t001 type table of t001.
class-data: it001 type table of t001.
class-data: xt001 like line of it001.
class-methods: change_table
exporting ex_wt001 type t001
changing im_t001 type t_t001.
endclass.
data: w_t001 type t001.
data: a_t001 type table of t001 with header line.
start-of-selection.
select * into table a_t001 from t001.
call method lcl_app=>change_table
importing
ex_wt001 = w_t001
changing
im_t001 = a_t001[] .
check sy-subrc = 0.
----
CLASS lcl_app IMPLEMENTATION
----
........ *
----
class lcl_app implementation.
method change_table.
loop at im_t001 into xt001.
concatenate xt001-butxt 'Changed'
into xt001-butxt separated by space.
modify im_t001 from xt001.
endloop.
ex_wt001 = xt001.
endmethod.
endclass.
</b>
Thanks&Regards
Naresh
05-12-2006 1:56 PM
Try this way:
call method class->method
exporting
gt_table = itab[].
Make sure that you define gt_table as a table type.
and itab is also of the same table type.
Regards,
Ravi
05-12-2006 1:59 PM
Hello Anand,
You can pass iternal table to Class-Method.
1.Goto transaction SE24 and display object.
2.Put your cursor on Method and click on Parameters.
this will show you all import export parameters list.
If you are writing new method just create new import parameter ( table name) and assigned associated type.
When you call this method in your program you can pass data as follows....just for your reference saome sample code...
CALL METHOD gcl_char_proxy-> execute_asyanchronous
EXPORTING
output = itab1.
where output is import parameter declare in Class-method ( here gcl_char_proxy-> execute_asyanchronous)
Hope this will help you.
Cheers,
Nilesh
05-12-2006 2:32 PM
Hi Anandaraja Ravi ,
Just defing the method parameter as TABLES, or STANDARD TABLE or as a kind of table you need.
Pay attention! in the OOP the table aren't heder line.
So when you call the method and your internal table has the header line you have to pass the table with [].
Eg.
call method yourMethod
exporting
table = yourTable[].
Bye
enzo
05-12-2006 6:01 PM
Anand,
Just do the following:
1:Declare a table type of internal table type.
2.Use this to declare an importing parameter(variable).
3.Use this method in your code where the internal table exists.
regards,
Bharat
PS:Pls. rewards pts. to answers.
05-10-2007 6:32 AM
Hi Anandaraja Ravi,
The way most easy to pass an internal table for a method in a local class as the following:
REPORT ztest_oo.
*----
*
CLASS teste DEFINITION
*----
*
........ *
*----
*
CLASS teste DEFINITION.
PUBLIC SECTION.
DATA: my_usr01 TYPE TABLE OF usr01,
conter TYPE i.
METHODS: set_table IMPORTING it_xyz TYPE ANY TABLE,
get_no_lines RETURNING value(my_lines) TYPE i.
ENDCLASS.
*----
*
CLASS TESTE IMPLEMENTATION
*----
*
........ *
*----
*
CLASS teste IMPLEMENTATION.
METHOD set_table.
DATA wa_usr01 TYPE usr01. "In ABAP Objects Can't use Header Line
MOVE it_xyz[] TO my_usr01[].
LOOP AT my_usr01 INTO wa_usr01.
...
Use the work area to manipulate the data of internal table
ENDLOOP.
ENDMETHOD.
METHOD get_no_lines.
DESCRIBE TABLE my_usr01 LINES my_lines.
ENDMETHOD.
ENDCLASS.
End of class
Global declaration of report
DATA r_obj TYPE REF TO teste.
DATA it_users TYPE TABLE OF usr01 WITH HEADER LINE.
DATA no_lines TYPE i.
START-OF-SELECTION.
Searching for users with output device LOCL
SELECT * FROM usr01
INTO TABLE it_users
WHERE spld = 'LOCL'.
Create a instance of teste
CREATE OBJECT r_obj.
Calling method to set the internal table my_usr01 of object
CALL METHOD r_obj->set_table EXPORTING it_xyz = it_users[].
Printing result
END-OF-SELECTION.
Calling the functional method which returns the number of lines in my_usr01
no_lines = r_obj->get_no_lines( ).
Print to screen the result of method
WRITE: / no_lines.
To pass a internal table for class developed in SE24, use the type ANY TABLE in the field 'Reference Type'. Don´t forget the declaration of internal table which receive the data passed by method.
Best Regards,
Leonardo Valente
08-06-2007 10:44 AM
Hi,
Another simple solution is to create a "table type" in the ABAP dictionary (transaction SE11) with the structure you need. In your method definition you can simple use this data type.
Example using the SAP table type MARA_TAB:
CLASS cl_test DEFINITION.
PUBLIC SECTION.
CLASS-METHODS do_test.
CLASS-DATA:
IMPORTING:
my_mara_itab TYPE MARA_TAB.
PRIVATE SECTION.
ENDCLASS.
Please Mark Helpful Answers.
Regards,
Dennis