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

Create variable with dynamic NAME

Astashonok
Participant
0 Likes
8,598

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?

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
3,809

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

16 REPLIES 16
Read only

Former Member
0 Likes
3,809

Hi Pavel,

Check this out - https://scn.sap.com/thread/53598.

BR.

Read only

naimesh_patel
Active Contributor
3,810

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

Read only

Astashonok
Participant
0 Likes
3,809

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?

Read only

0 Likes
3,809

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

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,809

Check if this helps , Any value you pass to varname, the variable of that name is created.

Read only

0 Likes
3,809

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. 

Read only

0 Likes
3,809

If you could explain you requirement clearly, may be we could help you. :-), What is the exact need for dynamic variable name ?

Read only

0 Likes
3,809

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.

Read only

0 Likes
3,809

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.

Read only

Astashonok
Participant
0 Likes
3,809

Thanks to everybody. I guess that it's impossible.

Read only

Former Member
0 Likes
3,809

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,

Read only

rosenberg_eitan
Active Contributor
0 Likes
3,809

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.

Read only

Former Member
0 Likes
3,809

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

Read only

0 Likes
3,809

Yes, I absolutely agree with you - this is a silly user requirement and a silly user. Now the problem is eliminated. Thanks to everybody

Read only

0 Likes
3,809

Hi,

I hope that you did not eliminate the user......

Somehow we still needs them.

Regards.

Read only

0 Likes
3,809

Ha-ha)) Yes, still need. But only some of them