‎2013 Jul 03 6:35 PM
The task is to create variable with dynamic name. NOT type(!) but namу! All the ways using cl_abap_typedescr and cl_abap_elemdescr classes that I saw are useless.
I want to implement something like this semantically:
CREATE DATA (name) TYPE var_type.
But this syntax is incorrect.
Is there any solution?
‎2013 Jul 03 10:28 PM
You can create an internal table with two columns - ID & DATA. In ID, track the name of the object and in DATA, have the data reference of the object.
Something like this would keep track of your data objects with the ID and also create different data objects with different types.
TYPES:
BEGIN OF ty_objs,
id TYPE char10,
data TYPE REF TO data,
END OF ty_objs.
DATA: t_objs TYPE STANDARD TABLE OF ty_objs.
DATA: ls_obj LIKE LINE OF t_objs.
DATA: lv_type TYPE string.
DO 5 TIMES.
ls_obj-id = sy-index.
lv_type = sy-index.
CONDENSE lv_type.
CONCATENATE 'CHAR' lv_type INTO lv_type.
CREATE DATA ls_obj-data TYPE (lv_type).
APPEND ls_obj TO t_objs.
ENDDO.
WRITE: 'done'.
But, question is why do you need dynamic name of the variable?
Regards,
Naimesh Patel
‎2013 Jul 03 7:18 PM
‎2013 Jul 03 10:28 PM
You can create an internal table with two columns - ID & DATA. In ID, track the name of the object and in DATA, have the data reference of the object.
Something like this would keep track of your data objects with the ID and also create different data objects with different types.
TYPES:
BEGIN OF ty_objs,
id TYPE char10,
data TYPE REF TO data,
END OF ty_objs.
DATA: t_objs TYPE STANDARD TABLE OF ty_objs.
DATA: ls_obj LIKE LINE OF t_objs.
DATA: lv_type TYPE string.
DO 5 TIMES.
ls_obj-id = sy-index.
lv_type = sy-index.
CONDENSE lv_type.
CONCATENATE 'CHAR' lv_type INTO lv_type.
CREATE DATA ls_obj-data TYPE (lv_type).
APPEND ls_obj TO t_objs.
ENDDO.
WRITE: 'done'.
But, question is why do you need dynamic name of the variable?
Regards,
Naimesh Patel
‎2013 Jul 04 8:33 AM
It's a user requirement. I saw the post which suggested Ankit but it's not the same.
As i see this is impossible. Is it?
‎2013 Jul 04 2:31 PM
Hi Pavel,
I did not verify the solution there, just saw it answered and posted here.
Found this but its not dynamic it promotes reusability -
define test.
data:&1(&2) type c.
end-of-definition.
test name1 3.
test name2 5.
-> decalres 2 variables name1 and name2.
-> See if you can play with these names and make them dynamic somehow ( i tried couldn't get it).
PS : Thanks to Kesavadas Thekkillath
BR.
Message was edited by: Ankit Maskara
‎2013 Jul 05 6:09 AM
‎2013 Jul 05 6:48 AM
Kesavadas Thekkillath wrote:
Check if this helps Dynamic Variable Declaration, Any value you pass to varname, the variable of that name is created.
Maybe I'm newbie but in your solution I saw nothing but declaring field-symbol with name <varname> as name of the selection-screen parameter. Debugger doesn't lie. Maybe it should be so (field-symbol with the name passed to SSC parameter) but it doesn't.
‎2013 Jul 05 7:43 AM
If you could explain you requirement clearly, may be we could help you. :-), What is the exact need for dynamic variable name ?
‎2013 Jul 05 8:04 AM
This is user requrement - he wants to name variables similar to table names due to usability.
MARA - ZMARA
MAKT - ZMAKT
and so on.
I wondering if it is technically possible.
‎2013 Jul 05 2:45 PM
Hi,
That's called naming conventions and it's something you have to respect while coding
A variable gets a name before you compile.
Regards,
David.
‎2013 Jul 25 6:02 AM
‎2013 Sep 19 10:47 AM
Have you thought about using structures? i.e. if the variable is to be named after tables, then you could in theory dynamically generate a structure and then describe the structure getting its name. Something like:
FIELD-SYMBOLS: <fs_structure> TYPE any,
<fs_comp> TYPE abap_compdescr.
CREATE DATA lv_ref TYPE (ls_file_contents-structure_name).
ASSIGN lv_ref->* TO <fs_structure>.
lcl_descr_ref ?= cl_abap_typedescr=>describe_by_name( <fs_structure> ).
Doing this you would be able to discover which table you are working with and you would be able to do things such as:
<fs_structure>-MATNR
where <fs_structure> is MARA.
Beyond this, short of writing an ABAP generator and then compiling the code on the fly I cannot see how you can go further than this.
Not really sure if this helps.
Cheers,
‎2013 Sep 19 11:23 AM
Hi,
Some how I am a little puzzled about "This is user requirement" .
In my experience I never encounter a user who dictate to me how the internal of a program should be written.
They are concern with input parameter, business logic, good performance ,Output layout and the reliably of the report.
Can you shed some light on the situation in this case.
Dynamic tables are solution not requirements .
Some how people tends to come out with complex solutions to simple problems.
Complex solutions are expensive !!!!
What are the real requirement in this case ? (like Kesavadas Thekkillath ask for)
Regards.
‎2013 Sep 19 11:28 AM
Hi,
I concur with Eitan. This can hardly be called a user requirement, it doesnt have any business merit to my knowledge.
What prompted the user to came up with this question? Or in other words what are the business requirements?
Kind regards, Rob Dielemans
‎2013 Sep 20 6:30 AM
Yes, I absolutely agree with you - this is a silly user requirement and a silly user. Now the problem is eliminated. Thanks to everybody
‎2013 Sep 20 6:41 AM
Hi,
I hope that you did not eliminate the user......
Somehow we still needs them.
Regards.
‎2013 Sep 20 8:55 AM