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 Method Call

Former Member
0 Likes
463

I have a procedure that frees GUI controls. I have a lot of controls, and rather than have many procedures, I'd like to have 1 procedure that I call, passing an object reference.

It doesn't appear that through dynamic method call, I can do this with an instance method.

Can someone suggest a way to do this, without having a procedure for each control?


<b>* I want to pass g_r_editor as a parameter</b>
IF NOT g_r_editor IS INITIAL.
    CALL METHOD g_r_editor->free
      EXCEPTIONS
        cntl_error        = 1
        cntl_system_error = 2
        OTHERS            = 3.
    IF sy-subrc NE 0.
      CALL FUNCTION 'POPUP_TO_INFORM'
        EXPORTING
          titel = text-011
          txt2  = space
          txt1  = text-005.
    ENDIF.
    FREE g_r_editor.
  ENDIF.

<b>Dynamic Method Call doc</b>

<a href="http://help.sap.com/saphelp_nw2004s/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm">http://help.sap.com/saphelp_nw2004s/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm</a>

2 REPLIES 2
Read only

uwe_schieferstein
Active Contributor
0 Likes
415

Hello Bjorn

I would suggest the following approach:

(1) Collect all your GUI control instances in an itab whose line type is TYPE REF TO cl_gui_control.

(2) Define your "free" method:

LOOP AT mt_gui_controls INTO lo_gui_control.
* See if gui control was text editor
  TRY.
    lo_textedit ?= lo_gui_control.
  CATCH cx_sy_move_cast_error INTO lo_error.
  ENDTRY.

  IF ( lo_error IS NOT BOUND ).
    lo_textedit->free.
*   ... your coding
    CONTINUE.
  ENDIF.


* See if gui control was tree
  TRY.
    lo_tree ?= lo_gui_control.
  CATCH cx_sy_move_cast_error INTO lo_error.
  ENDTRY.
* ...
ENDLOOP.

The FREE methods are redefined in the subclasses. However, you could try to call lo_gui_control->free directly because I am not sure if the coding of the superclass or subclass is called (I cannot test this at the moment).

Regards

Uwe

Read only

0 Likes
415

Thanks. I ended up doing something similar. Since all the objects are subclasses, I don't think I need to worry about casting errors.


...
  l_r_control = g_r_editor.
  PERFORM free_controls
    USING
      l_r_control.
...

FORM free_controls
  USING
     p_r_control TYPE REF TO cl_gui_control.

  IF NOT p_r_control IS INITIAL.
    CALL METHOD p_r_control->free
      EXCEPTIONS
        cntl_error        = 1
        cntl_system_error = 2
        OTHERS            = 3.
    IF sy-subrc NE 0.
      CALL FUNCTION 'POPUP_TO_INFORM'
        EXPORTING
          titel = text-011
          txt2  = space
          txt1  = text-002.
    ENDIF.
  ENDIF.
ENDFORM.                    "free_controls