‎2016 May 30 4:20 PM
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.
‎2016 May 30 4:48 PM
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?
‎2016 May 30 4:48 PM
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?
‎2016 May 30 4:52 PM
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.
‎2016 May 30 5:18 PM
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.
‎2016 May 30 5:50 PM
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
‎2016 May 30 5:01 PM
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.
‎2016 May 30 5:16 PM
Hi Mukesh,
sorry, but i think this will not work. i cannot create the object "submenu" more than once ?!?
br Martin
‎2016 May 30 5:21 PM
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.