Technical Articles
a tale of Robin Hood
Dear community, today we start with some code. Let’s see if the code already reveals what this blog is about.
REPORT zcity_of_nottingham.
DATA wanted_poster TYPE string VALUE '1000 silver coins reward for the capture of Robin Hood'.
START-OF-SELECTION.
DATA(robin_hood) = zcl_robin_hood=>get_instance( ).
robin_hood->examine_wanted_poster( wanted_poster ).
robin_hood->change_wanted_poster_stealthy( ).
robin_hood->examine_wanted_poster( wanted_poster ).
CLASS zcl_robin_hood DEFINITION
PUBLIC
FINAL
CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS get_instance
RETURNING
VALUE(result) TYPE REF TO zcl_robin_hood.
METHODS constructor.
METHODS change_wanted_poster_stealthy.
METHODS examine_wanted_poster
IMPORTING
wanted_poster TYPE string.
PRIVATE SECTION.
CLASS-DATA instance TYPE REF TO zcl_robin_hood.
ENDCLASS.
CLASS zcl_robin_hood IMPLEMENTATION.
METHOD constructor.
ENDMETHOD.
METHOD get_instance.
IF instance IS NOT BOUND.
CREATE OBJECT instance.
ENDIF.
result = instance.
ENDMETHOD.
METHOD change_wanted_poster_stealthy.
FIELD-SYMBOLS <wanted_poster> TYPE string.
ASSIGN ('(ZCITY_OF_NOTTINGHAM)WANTED_POSTER') TO <wanted_poster>.
IF sy-subrc = 0.
REPLACE 'Robin Hood' IN <wanted_poster> WITH 'Prince John'.
ENDIF.
ENDMETHOD.
METHOD examine_wanted_poster.
WRITE / wanted_poster.
IF wanted_poster CS 'Robin Hood'.
WRITE / 'Seriously?' COLOR COL_NEGATIVE.
ELSEIF wanted_poster CS 'Prince John'.
WRITE / 'Now it''s okay.' COLOR COL_POSITIVE.
ENDIF.
ENDMETHOD.
ENDCLASS.
example output of report
Let’s take a closer look at the code. Robin Hood is located in the city of Nottingham. There he discovers a poster: He is wanted … oh wonder ๐ After all, he has been offending the Sheriff of Nottingham for years. In addition he also said some not so nice things about Prince John. Ok, bothย did a lot for this situation. Maybe a mediator could help? ๐
Robin is annoyed by the wanted poster. He wants to change it “a little bit”. However, he cannot do this immediately and in public (a method with a CHANGING parameter to “correct” the wanted poster is not possible). Too many witnesses. He has to do it secretly. Fortunately, he’s skilled in such things …
Dear readers, I hope you were entertained well by this little story. How did this blog come about? This week I used the “dirty assign” for the first time in years to solve a problem in message control. I almost forgot that this technique exists. My blog is just a reminder.
This blog describes how it works. The technique can be used to access and change certain global data that is already in memory. Although you actually wouldnโt have access to them. The name “dirty assign” therefore fits well. As far as I know, this isn’t an officially supported statement. It works, but that can end at any time. So I avoid it as best as I can. But in some exceptional cases, it can be helpful (e.g. message control and older customer enhancement technology).
Best regards, thanks for reading and please stay healthy
Michael
P.S.: Please support the virtual wishing well.
P.S.S.: Not tired of reading blogs? Check this blog byย Damir Majer about Community Online events.
Not long ago I actually had to use the 'dirty assign'. It does come in handy even if that's rarely the case (which probably is good ^^).
Great blog and a nice way of telling the tale in combination with ABAP! ๐
I take fun very seriously ๐ A blog can be entertaining and informative at the same time. At least I keep trying ๐
Ah yes, I've used this trick back in 4.6c to change a flag in standard code from a user exit.ย Worked then, good to see it works now. But I can of course not recommend this approach.ย ๐
Cheers,
Rich Heilman