‎2011 Jul 14 11:02 AM
Hey guys,
I would like to know wether there is a way to hand an internal table over as formal parameter in a method?
For instance let's say we have the following method in a DEFINITION part of a class:
calc_something IMPORTING itab TYPE TABLE OF afvv
Of course, that doesn't work but that would be very handy. Is there a way of doing that?
Thanks for your answers in advanced.
Robert
‎2011 Jul 14 11:28 AM
What is the problem to have it as TYPE ANY TABLE?
Alternative would be using data references (TYPE REF TO DATA), then pass reference of table and dereference it inside method.
Regards
Marcin
‎2011 Jul 14 12:08 PM
thanks for your answers.
Alternative would be using data references (TYPE REF TO DATA), then pass reference of table and dereference it inside method.
@Marcin, could you provide an completely example ? I didn't catch what you trying to explain
‎2011 Jul 14 1:15 PM
Hello Robert,
If the formal param of the method is not dynamic(i.e., the internal table will always have a structure of AFVV), then I will do it this way:
1. Define a private table "type" in the class:
TYPES: ty_t_line TYPE TABLE OF afvv.2. Define the formal param using the type defined in Step1:
IMPORTING im_tab TYPE ty_t_line@Marcin, Neil: If the type of the actual param is fixed, do you think using generic table type(TYPE ANY TABLE) or data references(TYPE REF TO data) is required? Your inputs please.
BR,
Suhas
‎2011 Jul 14 1:52 PM
You need to have a table type either in the data dictionary or in the local types.
Example for local type:
TYPES: lyt_tab TYPE TABLE OF your_desired_type
CLASS zcl_test DEFINITION.
PUBLIC SECTION.
METHODS:
test
IMPORTING it_tab TYPE lyt_tab.
ENDCLASS.
Example for dictionary table type:
CLASS zcl_test DEFINITION.
PUBLIC SECTION.
METHODS:
test
IMPORTING it_tab TYPE dictionary_table_type.
ENDCLASS.
In the class builder (se24) when specyfying parameters, you need to have dictionary table type in order to pass internal table to the method. There are only LIKE, TYPE and TYPE REF TO options to specify, there is no TYPE TABLE OF.
‎2011 Jul 15 1:56 AM
Actually if you wanted to pass an internal table to a method in a global class you could easily enough define the internal table as a public attribute of the class.
You could then reference that internal table in your calling program.
EG:
In your global class...
public section.
types:
begin of gty_mara,
matnr type matnr,
maktx type maktx,
end of gty_mara .
data:
gt_mara type standard table of gty_mara .
In your calling program....
data: gt_mara type standard table of zcl_ng_demo=>gty_mara.
Passing / Returning the internal table
gt_mara = gr_demo->get_material_info( p_matnr ).
In your method you would use the LIKE typing when defining your parameter and point it towards gt_mara.
Of course this is only applicable if you creating your own custom global class.
I hope this clarifies things a little.
‎2011 Jul 24 2:15 PM
Hi Robert Peter,
just a sample for the reference thing.
data:
lr_data type ref to data.
get reference of gt_itab into lr_data.
call method process_table changing cr_table = lr_data.
** implementation
method process_table
changing cr_table type ref to data.
field-symbols:
<table> type table,
<record> type any,
<field> type any,
<vbak> type vbak.
* just some possibilities
ASSIGN cr_table->* to <table>.
LOOP AT <table> ASSIGNING <VBAK>.
* This will dump if passed reference is not of table with structure VBAK
<VBAK>-VBELN = 'xxx'.
ENDLOOP.
LOOP AT <table> ASSIGNING <record>.
ASSIGN FIELD 'VBELN' OF STRUCTURE <record> TO <field>.
<field> = 'xxx'.
* This will dump if no field VBELN is in the table structure - check SY-SUBRC first
ENDLOOP.
endmethod.Regards,
Clemens
‎2011 Jul 24 2:29 PM
‎2011 Jul 24 4:22 PM
Hello Clemens,
If the type of the actual param is fixed(rather "not dynamic"), then what is the need to pass a data reference to the method?
BR,
Suhas
‎2011 Jul 24 4:23 PM
yes,Robert, I just wrote just a sample for the reference thing.
and pretty much shortened.
Regards,
Clemens
‎2011 Jul 24 4:27 PM
Hi Suhas,
none. If you do not check the pass-by-value flag, it is passed by reference anyway. But you have to define a table type if there is nothing useful in DDIC. For public methods, the type must be declared in DDIC, otherwise in local TYPES of class.
Regards,
Clemens
‎2011 Jul 25 8:05 AM
‎2011 Jul 14 11:29 AM
Hi,
No, you can not do that. Everything in the interface of a method requires a data dictionary reference. What you can do is, create a structure in the dictionary using SE11 and then use this structure to create a table types in the dictionary. Now you can define a variable in the method parameter in reference to this table type, and pass your internal table (use [ ] sign beside the name of the table) to this method in your calling program.
CALL METHOD abc->test_method
EXPORTING
it_vbap = lt_vbap[ ]
it_ztab = itab[ ]
it_idoc_data = edidd[ ]
IMPORTING
error_flag = lv_flag
......
Tried and tested
Cheers,
Anid
‎2011 Jul 14 11:47 AM
Actually, it is possible. You just need to see Marcins' answer.