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 call: only pass supplied parameters

Former Member
0 Likes
2,321

Hi dear colleagues,

I want to call a method within my method, but only pass the parameters that are supplied.

E.g. method1 has 3 optional parameters and method2 has the same 3 optional parameters.

From within method1, I want to call method2, but only passing the parameters that are supplied (because in method2 I want to use the IF <parameter> IS SUPPLIED. statement).

I'm currently solving this by using the PARAMETERS-TABLE:

IF imv_param1 IS SUPPLIED.

  lw_param-name = 'IMV_PARAM1'.

  lw_param-kind = cl_abap_objectdescr=>exporting.

GET REFERENCE OF imv_param1 INTO lw_param-value.

INSERT lw_param INTO TABLE li_params.

ENDIF.

IF imv_param2 IS SUPPLIED.

lw_param-name = 'IMV_PARAM2'.

lw_param-kind = cl_abap_objectdescr=>exporting.

GET REFERENCE OF imv_param2 INTO lw_param-value.

INSERT lw_param INTO TABLE li_params.

ENDIF.

IF imv_param3 IS SUPPLIED.

lw_param-name = 'IMV_PARAM3'.

lw_param-kind = cl_abap_objectdescr=>exporting.

GET REFERENCE OF imv_param3 INTO lw_param-value.

INSERT lw_param INTO TABLE li_params.

ENDIF.

CALL METHOD ('my_object->method2')

  PARAMETER-TABLE li_params.


I was hoping that there is maybe an easier solution.


1 ACCEPTED SOLUTION
Read only

amy_king
Active Contributor
0 Likes
1,889

Hi Bjorn,

I think you already have an elegant solution. A less elegant solution that uses fewer lines of code could be to use IS NOT INITIAL in method2 instead of IS SUPPLIED. Then you could pass all parameters indiscriminately from method1 to method2.

Cheers,

Amy

Hi dear colleagues,

I want to call a method within my method, but only pass the parameters that are supplied.

E.g. method1 has 3 optional parameters and method2 has the same 3 optional parameters.

From within method1, I want to call method2, but only passing the parameters that are supplied (because in method2 I want to use the IF <parameter> IS SUPPLIED. statement).

I'm currently solving this by using the PARAMETERS-TABLE:

IF imv_param1 IS SUPPLIED.

  lw_param-name = 'IMV_PARAM1'.

  lw_param-kind = cl_abap_objectdescr=>exporting.

GET REFERENCE OF imv_param1 INTO lw_param-value.

INSERT lw_param INTO TABLE li_params.

ENDIF.

IF imv_param2 IS SUPPLIED.

lw_param-name = 'IMV_PARAM2'.

lw_param-kind = cl_abap_objectdescr=>exporting.

GET REFERENCE OF imv_param2 INTO lw_param-value.

INSERT lw_param INTO TABLE li_params.

ENDIF.

IF imv_param3 IS SUPPLIED.

lw_param-name = 'IMV_PARAM3'.

lw_param-kind = cl_abap_objectdescr=>exporting.

GET REFERENCE OF imv_param3 INTO lw_param-value.

INSERT lw_param INTO TABLE li_params.

ENDIF.

CALL METHOD ('my_object->method2')

  PARAMETER-TABLE li_params.


I was hoping that there is maybe an easier solution.


7 REPLIES 7
Read only

amy_king
Active Contributor
0 Likes
1,890

Hi Bjorn,

I think you already have an elegant solution. A less elegant solution that uses fewer lines of code could be to use IS NOT INITIAL in method2 instead of IS SUPPLIED. Then you could pass all parameters indiscriminately from method1 to method2.

Cheers,

Amy

Read only

Former Member
0 Likes
1,889

Thank you Amy, I also thought of doing that, but I also want to use it for exporting parameters. These are most of the time initial.

The main problem is that I'm using dynamic tables, so I need to know if the table was supplied before I can add a line to it:

IF exi_table IS SUPPLIED.

  APPEND INITIAL LINE TO exi_table

      ASSIGNING <lw_table_line>.

*Fill in the data for <lw_table_line>.
ENDIF.

Read only

wol
Active Participant
0 Likes
1,889

Hello Bjorn,

I would suggest to import the reference of the itab. E. g. you could have a parameter im_my_itab_ref type ref to table and check if it is bound.

Example:

Does it help?

Regards,

Stefan

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,889

Hello Bjorn,

I agree with that your's is an elegant solution.

But i'm a lil' bit confused , for method1 the params are importing & for method2 they are exporting - please correct me if i am wrong.

BR,

Suhas

Read only

Former Member
0 Likes
1,889

Yes, I'm sorry, my mistake.

It's because I have method1 and method2, where the parameters are importing and I have method3 and method4 where the parameters are exporting.

In method2, I can do IF imv_param1 IS NOT INITIAL....

But in method4, I can't use IF exv_param1 IS NOT INITIAL, because in my program, if I provide the parameters, they will always be empty.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,889

But in method4, I can't use IF exv_param1 IS NOT INITIAL, because in my program, if I provide the parameters, they will always be empty.

That's the reason i was confused So your requirement is that if the IMPORTING param tables (of methods 1 & 2) are not SUPPLIED, you don't want to pass them as the actual param to the EXPORTING tables of methods 3 & 4 - hope i understand your requirement correctly.

I assume that these methods do not belong to the same class. If possible you can post relevant portion of your code for better analysis.

BR,

Suhas

Read only

Former Member
0 Likes
1,889

No, actually method1 calls method2 (both have IMPORTING parameters) and method3 calls method4 (both have EXPORTING parameters) and indeed method2 is in a different class than method1.

Here's some more info:

- Class1 is a public class. Class2 is a protected class, with class1 defined as friend.

- Suppose Class2 has 3 attributes AT_IOD_TABLE1, AT_IOD_TABLE2 and AT_IOD_TABLE3, which are private and TYPE REF TO DATA (with an internal table with a dynamic structure behind each of them)

- In class2, there is a protected method ME_IO_GET_DATA with tree exporting parameters EXI_DATA1, EXI_DATA2, EXI_DATA3 of type STANDARD TABLE and an importing parameter IMV_ID. EXI_DATA1 should contain data from AT_IOD_TABLE1, EXI_DATA2 should contain data from AT_IOD_TABLE2 and EXI_DATA3 should contain data from AT_IOD_TABLE3, but only if the parameters are supplied. Here is where I do:

IF exi_data1 IS SUPPLIED.

  APPEND INITIAL LINE TO exi_data1

      ASSIGNING <lw_table_line>.

*Fill in the data for <lw_table_line>,

*reading lines from AT_IOD_TABLE1 based on IMV_ID
ENDIF.


- In class1, there is a public method ME_IU_GET_DATA, with the same three exporting parameters (+ some other exporting parameters that are irrelevant here) and also an importing parameter IMV_ID. Here I want to call method ME_IO_GET_DATA of class2.