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

dynamic naming of instance reference variable

Former Member
0 Likes
1,105

Hi,

How to dynamically name instances of a particular class. 

For example: I have 4 screens 9000,9001,9002,9003.

data: lv_name type string.

field_symbols: <fs_objref> TYPE REF TO cl_gui_custom_container.

CREATE OBJECT GR_CUST_CONT_9000,

CREATE OBJECT GR_CUST_CONT_9001,

CREATE OBJECT GR_CUST_CONT_9002,

CREATE OBJECT GR_CUST_CONT_9003,

Instead of this, I need to assign a variable lv_name.

CONCATENATE 'GR_CUST_CONT_'  lv_dynnr INTO lv_name.

assign lv_name TO <fs_objref>.

CREATE OBJECT <fs_objref>

Regards,

DPM

3 REPLIES 3
Read only

Abhijit
Explorer
0 Likes
722

Hi DPM,

I think, instead of that it would be better to have internal table containing object references (object collection).. this internal table can have field 'Sceen ID' and "Reference to custom container (REF TO)'.

Then you can instaintiate the object per screen id and add it to the object collection.. while retriving, simply get the required object by referring screen id field from the internal table..

Best Regards,

Abhijit

Read only

alex_campbell
Contributor
0 Likes
722

Field names in an ABAP program are only there to make the programmer's life easier. If you find yourself building logic around the names of fields which you declare in your program, you're doing something wrong. Whatever relationship you're trying to define in the names of your fields, you should instead be defining in the values of a field.

In your specific situation, you shouldn't define the relationship between a screen number and it's custom container object by naming the custom container object from that screen number. Instead, as Abhijit said, you should create a table that relates a screen number (in one column) to the custom container object) in another column.

Read only

alex_cook
Active Participant
0 Likes
722

Howdy,

For what its worth, I don't think there's any problem using naming convention to achieve what seems to be a repetitive task.

So your code should work like this (change highlighted in bold)

CONCATENATE 'GR_CUST_CONT_'  lv_dynnr INTO lv_name.

assign (lv_name) TO <fs_objref>.

CREATE OBJECT <fs_objref>

You may also want to consider encapsulating the Create object call in a method or subroutine and passing the object reference.

Cheers

Alex