Problem solve Get help with specific problems with your technologies, process and projects.

Benefits of object methods as subroutines

In version 4.6 there is an advantage to using object methods rather than the classical form subroutines or function modules.

In version 4.6 there is an advantage to using object methods rather than the classical "form" subroutines or function...

modules. The result of the method call can be directly assigned. That means that the call can be used as a parameter in an operation. But, there are 2 watch-its: 1) There can only be 1 returning parameter 2) The only other parameter allowed are importing parameters (as many as you want) Net result: shorter, clearer, and better documented code. (This tip does not apply to versions prior to 4.0, and may not work on versions prior to 4.6)


REPORT  z_object_subroutines.


* class definition and implementation
CLASS my_class DEFINITION.

  PUBLIC SECTION.

    METHODS: method1 IMPORTING i_char TYPE char1
                     RETURNING value(r_char) TYPE char1,

             method2 RETURNING value(r_val) TYPE i.

ENDCLASS.


CLASS my_class IMPLEMENTATION.

  METHOD method1.
    " processing goes here
    " return the value
    r_char = 'X'.
  ENDMETHOD.

  METHOD method2.
    " processing goes here
    " return the value
    r_val = 5.
  ENDMETHOD.

ENDCLASS.


* global data
DATA: returned_value TYPE char1,
      returned_amt TYPE i,
      calc_amt TYPE i.

DATA: obj TYPE REF TO my_class.


* program events
START-OF-SELECTION.

  CREATE OBJECT obj.

* the classical (old) way there has to be a variable (delcared
* globally) which is assigned in the subroutine call - then
* the operation is performed.

  PERFORM form1  USING 'X'
                 CHANGING returned_value.
  IF returned_value = 'X'.
    " do something
  ENDIF.

  PERFORM form2 CHANGING returned_amt.
  calc_amt = 5 + returned_amt.

* with the object methods you can do direct comparisons of the
* returned value as well as direct assignments

  " direct assignment
  returned_value = obj->method1( 'X' ).

  " direct comparison
  IF obj->method1( 'X' ) = 'X'.
    " do something
  ENDIF.

  " operation parameter
  calc_amt = 5 + obj->method2( ).


* classical (old) form subroutines
FORM form1 USING i_char
           CHANGING r_char.
  " processing goes here
  " return the value
  r_char = 'X'.
ENDFORM.

FORM form2 CHANGING r_val.
  " processing goes here
  " return the value
  r_val = 5.
ENDFORM.

This was last published in October 2002

Dig Deeper on SAP implementation and upgrades

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close