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

Creating a variable of specified type dynamically

raghavendra_subhash
Product and Topic Expert
Product and Topic Expert
0 Likes
763

Hi,

I have requirement where in I need to return a variable of specified data type either through a method or function call.

DATA: var_typ type char30 value 'I',

var_val type char30 value '123'.

To create a generic function or method which would return a variable of type 'I' with value '123', taking the var_typ and var_val as input parameter.

This is just an example, actual I have a internal table with multiple records and each record has a different data type, for each record a different variable needs to be created i.e. variable should be a return type and not a exporting type.

More information about the requirement

Invoke methods dynamically.

Given:

1) You have a table with 2 fields (data type and value)

2) You have the class name and the method name (assume that method has one importing parameter).

3) Not to declare a variable of type importing or exporting in the program.

Information already gathered.

1) Knows how to create objects dynamically (CREATE OBJECT obj_add TYPE (class_name)).

2) Knows how to invoke a method dynamically (CALL METHOD obj_add->(method_name) PARAMETER-TABLE ptab).

It would be helpful if you can provide the piece of code or guide me on how this can be implemented in ABAP.

Thanks,

Raghavendra

3 REPLIES 3
Read only

Former Member
0 Likes
669

Hi

Why dont you use a field symbol

e.g FIELD-SYMBOLS : value type any.

You can assign any value to this variable.

Check this link for more information.

http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3860358411d1829f0000e829fbfe/frameset.htm

Read only

Former Member
0 Likes
669

DATA:

ref_itab TYPE REF TO data,

ref_wa TYPE REF TO data.

FIELD-SYMBOLS:

<fs_itab> TYPE ANY TABLE,

<fs_wa> TYPE ANY,

<fs_comp> TYPE ANY.

PARAMETERS:

pa_tab TYPE dd02l-tabname.

Start-of-Selection.

CREATE DATA ref_itab TYPE STANDARD TABLE OF (pa_tab) WITH NON-UNIQUE DEFAULT KEY.

ASSIGN ref_itab->* to <fs_itab>.

SELECT * FROM (pa_tab) INTO TABLE <fs_itab>.

CREATE DATA ref_wa LIKE LINE OF <fs_itab>.

ASSIGN ref_wa to <fs_wa>

LOOP AT <fs_itab> into <fs_wa>.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs_comp>.

IF sy-subrc NE 0.

NEW-LINE.

EXIT.

ENDIF.

WRITE <fs_comp>.

ENDDO.

ENDLOOP.

Read only

raghavendra_subhash
Product and Topic Expert
Product and Topic Expert
0 Likes
669

Hi,

Thanks for quick responce. I tried using the field symbols, but was not possible. I pasted a sinp of the code that actual work i.e. without using the field symbol.

DATA: ptab type abap_parmbind_tab,

ptab_line TYPE abap_parmbind.

DATA: class_name type CHAR30 VALUE 'ZCL_ADD,

method_name type CHAR30 VALUE 'ADD'.

DATA: obj type ref to OBJECT.

DATA: IV_var1 TYPE I.

DATA: IV_var2 TYPE I.

DATA: EV_OUT TYPE I.

*

IV_var1 = 1.

IV_var2 = 2.

EV_OUT = 0.

*

create object obj type (class_name).

ptab_line-name = 'IV_var1'.

ptab_line-kind = CL_ABAP_OBJECTDESCR=>EXPORTING.

GET REFERENCE OF IV_var1 INTO ptab_line-value.

INSERT ptab_line into table ptab.

*

ptab_line-name = 'IV_var2'.

ptab_line-kind = CL_ABAP_OBJECTDESCR=>EXPORTING.

GET REFERENCE OF IV_var2 INTO ptab_line-value.

INSERT ptab_line into table ptab.

*

ptab_line-name = 'EV_out'.

ptab_line-kind = CL_ABAP_OBJECTDESCR=>IMPORTING.

GET REFERENCE OF EV_out INTO ptab_line-value.

INSERT ptab_line into table ptab.

*

*Then you can use

CALL METHOD obj->(method_name) PARAMETER-TABLE ptab.

*

WRITE: 'this test program'.

WRITE: EV_out.

Here I have declared a variables (iv_var1, iv_var2, ev_out) of the method ADD. Instead of declaring the variable, I need a generic function which can read from data from the table and return me variable with value., so that I can insert it to the ptab.

Thanks,

Raghavendra