Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Method: Internal table as a formal parameter ?

0 Likes
3,117

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

13 REPLIES 13
Read only

MarcinPciak
Active Contributor
0 Likes
2,354

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

Read only

0 Likes
2,354

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,354

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

Read only

0 Likes
2,354

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.

Read only

0 Likes
2,354

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.

Read only

0 Likes
2,354

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

Read only

0 Likes
2,354

Hey Clemens,

that is a calll-by-reference example, isn't it?

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,354

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

Read only

0 Likes
2,354

yes,Robert, I just wrote just a sample for the reference thing.

and pretty much shortened.

Regards,

Clemens

Read only

0 Likes
2,354

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

Read only

0 Likes
2,354

Thanks a lot!

Read only

Former Member
0 Likes
2,354

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

Read only

0 Likes
2,354

Actually, it is possible. You just need to see Marcins' answer.