‎2007 Feb 02 10:21 AM
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
‎2007 Feb 03 8:46 PM
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