Application Development 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: 

Creating a variable of specified type dynamically

0 Kudos
76

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

1 REPLY 1

Former Member
0 Kudos
38

1.)You always can return as data type 'table'.

2.)You can return as ref to data. For that, you need to create a variable dynamically in your method, compare:

DATA: lv_var TYPE i VALUE 19768,

l_dref1 TYPE REF TO data,

l_dref2 TYPE REF TO data.

FIELD-SYMBOLS: <fs_1> TYPE i, <fs_2> TYPE i.

  • Get name and value of import parameter

GET REFERENCE OF lv_var INTO l_dref1.

CREATE DATA l_dref2 TYPE i.

ASSIGN l_dref2->* TO <fs_2>.

ASSIGN l_dref1->* TO <fs_1>.

<fs_2> = <fs_1>.

re_va2 = l_dref2.

Regards,

Thomas