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 OBJECT dynamic

martin_svik2
Participant
0 Likes
1,161

hi there,

i have an CREATE OBJECT in my coding, which looks like this:

  DATA: submenu            TYPE REF TO cl_ctmenu,..........

................

...............

   CREATE OBJECT submenu.

...............

...............

so far, so good. now i have the following requirement. inside a loop, at every new occurence of field "year" i want to do a create object in a dynamic way....like create object year_2015, create object year_2014, create object year_2013, .................each of them as type ref to cl_ctmenu.

loop at it_table. 

    at new year.

    create object -> this has to be done dynamic with the name of the content of field year -> how to do this ?

    endat.

endloop.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,124

What's "year_2015", "year_2014", etc.? Are these classes (maybe sub-classes of CL_CTMENU?) or is this some kind of data? What do you want to do with the created objects?

7 REPLIES 7
Read only

Former Member
0 Likes
1,125

What's "year_2015", "year_2014", etc.? Are these classes (maybe sub-classes of CL_CTMENU?) or is this some kind of data? What do you want to do with the created objects?

Read only

0 Likes
1,124

it is for defining menues inside a dropdown button in ALV. for each submenu there has to be a create object.

see screenshot: for each menu (each year) there has to be a own create object, in this example for 2015 and 2016. this has to be dynamic, it depends on the years which are in the internal table.

Read only

0 Likes
1,124

So, you want to create for each year an own context menu entry, right? As far as I see for CL_CTMENU you have to add the sub menu entries by method ADD_SUBMENU. This way I guess you have to do the following:

LOOP AT it_table.

     AT NEW year.

          CREATE OBJECT submenu.

          " add some functions for the year

          submenu->add_function( ... ).

          ...

          " add submenu

          mainmenu->add_submenu( menu = submenu text = |{ it_table-year }| ).

     ENDAT.

ENDLOOP.

Or for new syntax fans (NW 7.4 >=SP08):

LOOP AT it_table ASSIGNING FIELD-SYMBOL(<ls_line>)

     GROUP BY year INTO DATA(lv_year).

     submenu = NEW #( ).

    

      " add some functions for the year

      submenu->add_function( ... ).

       ...

       " add submenu

       mainmenu->add_submenu( menu = submenu text = CONV #( lv_year ) ).

ENDLOOP.

Read only

0 Likes
1,124

Thank you Armin, that works !

I had kind of a logic problem with the create object statement ! it was an "node in my brain"

br Martin

Read only

MukeshKumar
Participant
0 Likes
1,124

Hi Former Member !

You can declare internal table of object references like:


DATA: it_submenu TYPE TABLE OF REF TO cl_ctmenu.

and then, modify your above logic to:


LOOP AT it_table.

    AT NEW year.

          CREATE OBJECT submenu.

          APPEND submenu TO it_submenu.

    ENDAT.

ENDLOOP.

And later on, you can process objects' table by LOOPing on it_submenu.

Best Regards.

Read only

0 Likes
1,124

Hi Mukesh,

sorry, but i think this will not work. i cannot create the object "submenu" more than once ?!?

br Martin

Read only

0 Likes
1,124

Yes you can. The variable SUBMENU is only a reference to your object. With CREATE OBJECT you create a new object and using the same variable, you simply reference it to the new created object. If no other reference exists for your old object, it will be discarded by the garbage collector at some point in time.