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

Type Conflict when calling class and method dynamically

Former Member
0 Likes
3,748

Hi OO experts,

i want to call  a method dynamically. My problem ist, that I get a type error when processing.

* Importing Parameter SAP-Value **************************************
           ls_param-name = 'IM_SAPVAL'.
           ls_param-kind = cl_abap_objectdescr=>importing.
           GET REFERENCE OF <lv_sapval> INTO ls_param-value.
           INSERT ls_param INTO TABLE lt_param.

           ......


          TRY.
             CALL METHOD (lv_class)=>(ls_xtime_fields-convert_method)
               PARAMETER-TABLE
                 lt_param
               EXCEPTION-TABLE
                 lt_excep.
             CATCH cx_sy_dyn_call_error INTO lr_excep.
               lv_extxt = lr_excep->get_text( ).
               MESSAGE lv_extxt TYPE 'I'.
           ENDTRY.

I am reading parameters from customizing table and it`s Type CHAR.

Method expects Type BEGDA. Other methods could have more different types like UZEIT or DECIMAL, which values are type char, too.

Is there any way to change type dynamically? I tried Type ANY, but get still the error about wrong type.

Cheers

Dieter

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
2,940

When you assign the value to the data reference, it also carries its type. If you want to pass them as CHAR, you need to convert the values in CHAR before getting the reference.

DATA: lv_i TYPE i VALUE 1.
DATA: lv_c TYPE char10.
DATA: o_data  TYPE REF TO data.
lv_c = lv_i.
GET REFERENCE OF lv_i INTO o_data.

Is it possible for you to create a smaller example of your class with required method and its parameters, which we can execute to see the error?

Regards,
Naimesh Patel

14 REPLIES 14
Read only

naimesh_patel
Active Contributor
0 Likes
2,941

When you assign the value to the data reference, it also carries its type. If you want to pass them as CHAR, you need to convert the values in CHAR before getting the reference.

DATA: lv_i TYPE i VALUE 1.
DATA: lv_c TYPE char10.
DATA: o_data  TYPE REF TO data.
lv_c = lv_i.
GET REFERENCE OF lv_i INTO o_data.

Is it possible for you to create a smaller example of your class with required method and its parameters, which we can execute to see the error?

Regards,
Naimesh Patel

Read only

0 Likes
2,940

Hi Naimesh

I did two versions. First idea was to call a method, to build param tables.

IF ls_xtime_fields-convert_method IS NOT INITIAL.
           me->create_paramtab(
             EXPORTING
               im_ex_xtval  = <lv_sapval>
               im_im_sapval = ls_xtime_fields-sap_name
               im_im_param1 = ls_xtime_fields-convert_param1
             IMPORTING
               ex_gt_param  = lt_param
               ex_gt_excep  = lt_excep ).

Parameter table contains this:

rowNAME[C(30 )]KIND[C(1 )]VALUE[Fref]My Row: Values and Types
1IM_SAPVALIFREED STACK:{A:15*\TYPE=BEGDA}<LV_SAPVAL> = 20101001 Type D(8)
2IM_PARAM1IFREED STACK:{A:16*\TYPE=CHAR255}LS_XTIME_FIELDS-CONVERT_PARAM1 = "DD.MM.YYYY" C(255)
3EX_XTVALRFREED STACK:{A:17*\TYPE=ZHR_DE_SAP_NAME}LS_XTIME_FIELDS-SAP_NAME = "BEGDA" C(32)
ZHR_DE_SAP_NAME = char32





This was more worse, than the 2nd try: I put the functionality from method create_paramtab, directly  before dynamic method call. See next Post.

Read only

0 Likes
2,940

First the code of the 2nd version:

* Importing Parameter SAP-Value **************************************
           ls_param-name = 'IM_SAPVAL'.
           ls_param-kind = cl_abap_objectdescr=>importing.
          GET REFERENCE OF <lv_sapval> INTO ls_param-value.  "<-- not working
           INSERT ls_param INTO TABLE lt_param.

* Importing Parameter formating *****************************
           IF ls_xtime_fields-convert_param1 IS NOT INITIAL.
             ls_param-name = 'IM_PARAM1'.
             ls_param-kind = cl_abap_objectdescr=>importing.
             GET REFERENCE OF ls_xtime_fields-convert_param1 INTO ls_param-value.
             INSERT ls_param INTO TABLE lt_param.
           ENDIF.

* Exporting Parameter Export-Format ***********************************
           ls_param-name = 'EX_XTVAL'.
           ls_param-kind = cl_abap_objectdescr=>returning.
           GET REFERENCE OF
<lv_xtime>
INTO ls_param-value.
           INSERT ls_param INTO TABLE lt_param.

* Exception Tabelle füllen *******************************************
           ls_excep-name = 'OTHERS'.
           ls_excep-value = 4.
           INSERT ls_excep INTO TABLE lt_excep.

           TRY.
             CALL METHOD (lv_class)=>(ls_xtime_fields-convert_method)
               PARAMETER-TABLE
                 lt_param
               EXCEPTION-TABLE
                 lt_excep.
             CATCH cx_sy_dyn_call_error INTO lr_excep.
               lv_extxt = lr_excep->get_text( ).
               MESSAGE lv_extxt TYPE 'I'.
           ENDTRY.

Now lt_param contains this:

rowNAME[C(30 )]KIND[C(1 )]VALUE[FREF]
1IM_SAPVALI->20101001
2IM_PARAM1I->DD.MM.YYYY
3EX_XTVALR->

The method which is called is:

zcl_conv=>convert_date( IMPORTING im_sapval = <lv_sapval> im_param1 = ls_xtime_fields-convert_param1

                        RETURNING ex_xtval = <lv_xtime>).

Method has this parameter:


PARAMETERTYPE
IVALUE( IM_SAPVAL )TYPE BEGDA (I tried different types)
IVALUE( IM_PARAM1 )TYPE CHAR10
RVALUE( EX_XTVAL )TYPE CHAR10


method CONVERT_DATE.
   WRITE im_sapval TO re_xtval USING EDIT MASK im_param1.
endmethod.

I am afraid, that the same problem exsits for following parameters.

Cheers

Dieter

Read only

0 Likes
2,940

Hello Dieter,

I see 2 potential issues.

1. In the first approach, FREED STACK:{A:16*\TYPE=CHAR255} - >means the memory no longer exists. You are passing reference of local variables,but these variables do not have memory outside of their method call.

2. In the second approach, You should use the create data statement for every table row ( Value field). here is a code snippet. This might work.

Ex : Data : lt_param type  abap_parmbind_tab.

Field-symbols : <fs_param> type  abap_parmbind.,

                         <fs_val> type any.

append initial line to lt_param assigning <fs_param>.

create data <fs_param>-value type ( "Your Dynamic type " ).

Assign <fs_param>-value->* to <fs_val>.

<fs_val> = "the Value".

Thanks,

Venkat.

Read only

0 Likes
2,940

Hi Venkant,

it sounds great and I tried it directly - but got one new issue

append initial line to lt_param assigning <fs_param>.

lt_param is type abap_parmbind_tab and this is a hashed table. Appending is not possible.

I changed it:


INSERT INITIAL LINE INTO TABLE lt_param ASSIGNING <fs_param>.

Edit:

It`s working and lt_param looks good, but I still get the same error message. Table contains this:

1    IM_SAPVAL    I    ->20101001

2    IM_PARAM1    I    ->DD.MM.YYYY

3    RE_XTVAL      R    ->

<FS_VAL> is 20101001 and Type D(8) - That what the methods expects.

Cheers

Dieter

BTW.: So much respondse in such a short time Thank you all!

Read only

0 Likes
2,940

Hello Dieter,

Sorry, I never realised that it is an Hashed table type. I am so used to adding an inital line and assigning them to field symbols ( for std tables ) , that I never realized it would not work for Hashed tables .

Anyways, here is  a code snippet which i tried in my system.

  DATA : lt_param TYPE  abap_parmbind_tab,
         lwa_param TYPE abap_parmbind.


  FIELD-SYMBOLS : <fs_val>   TYPE ANY.

  lwa_param-name = 'IM_PARAM1'
  lwa_param-kind = cl_abap_objectdescr=>importing.
  CREATE DATA lwa_param-value TYPE ('Your data type').
  ASSIGN lwa_param-value->* TO <fs_val>.
  <fs_val> = "The value".
  INSERT lwa_param INTO TABLE lt_param.

...Follow the same sequence of steps for all the parameters.

Let me know if you encounter any issues. '

Thanks,

Venkat

Read only

0 Likes
2,940

Hello Dieter,

Could you paste the error message description.

Thanks,

Venkat.

Read only

0 Likes
2,940

Hi Venkat

I need to quit, for today - hours enough. I guess you are in an other time zone

This ist my lt_param - looks like it should

This is the method called:

So nothing difficult. But if this is not working, I don`t need to implement all other convert methods this way. I am Working on EHP 0. May this be a problem?

Hope to read you tomorrow

Cheers

Dieter

Read only

0 Likes
2,940

Hello Dieter,

Guess what , the issue is

ls_param-kind = cl_abap_objectdescr=>importing.

It should be

ls_param-kind = cl_abap_objectdescr=>Exporting

I tried the same scenario in my system and I too got the same error. I was puzzled . Then I realized, that  I was passing them as Importing parameters, instead of Exporting. That solved the issue.

Its now time to leave office in my Time zone . Your post did make me learn some new things. Thanks.

Thanks,

Venkat.

Read only

0 Likes
2,940

Hi Venkat

Yeah it`s working now - great.

I am excited - this report will get the most flexible interface I did, when all is working.

Thank you very much.

Cheers

Dieter

BTW - I changed returning to exporting parameter - returning seems not to work

Read only

Former Member
0 Likes
2,940

    Hello,

          In your code :


            ls_param-name = 'IM_SAPVAL'.

           ls_param-kind = cl_abap_objectdescr=>importing.

           GET REFERENCE OF <lv_sapval> INTO ls_param-value.      "dont pass the reference instead use

                                                                                                                       "MOVE <lv_sapval> TO ls_param-value

           INSERT ls_param INTO TABLE lt_param.

Thank You.

Read only

0 Likes
2,940

Hi Ashis

I tried this already , but get an dump because I am not allowed to "Convert from type "D" (date) to "l" (reference).

Cheers

Dieter

Read only

0 Likes
2,940

Hi Dieter,

     is the type I you are referring to is integer  ? Because type D can be converted to type I without error.

Thank You.

Read only

0 Likes
2,940

TYPE is not I (big i) Type is l (small L) - I should have changed Font - sorry

Cheers

Dieter