MichaelB.
7/28/2018 - 10:18 AM

CALL a METHOD

The space in between the brackets signifies that no parameters are being imported or exported (passed) to/from the method. Instance methods use a dash ->, and static methods use an equal sign => when being called. RULES: There must not be a space before the opening parenthesis. There must be a space after the opening parenthesis. If there is a parameter there must be a space on either side. If a method imports only 1 parameter, place the param inside the parenthesis If a method imports more than 1 parameter, list the param in pairs (often listed underneath eachother for clarity). If there are parameters other than importing, list the type of param passing (from the calling program's perspective).

Parameter passing types of the calling program correspond to the parameter types in a method, but they are not the same: METHOD DEFINTION: IMPORTING/EXPORTING/CHANGING/RETURNING corresponds to METHOD CALL: EXPORTING/IMPORTING/CHANGING/RECEIVING

You can define a parameter as OPTIONAL: then it does not have to be passed into the method when calling the method. That way we can set a default value for the attribute in the method if the calling program did not specify a value. You can also specify a VALUE for attributes in your DATA statement.

*Call a static method
CALL METHOD classname=>methodname. "Old way
classname=>methodname( ). "New way

*Call an instance method
objectname->methodname( ).

*Calling a method that only imports 1 parameter
objectname->methodname( data ).

*Calling a method that imports more than 1 parameter
Formal param = actual param
objectname->methodname( param1 = data1 param2 = data2 ).

*Calling a method with parameters other than importing
objectname->methodname( EXPORTING param1 = data1 param2 = data2 
                       IMPORTING param3 = data3 param4 = data4      "This is a little strange - Have to read right to left
                       CHANGING  param5 = data5 param6 = data6 ).
                     
********************************************************
* Practical example

START-OF-SELECTION.

  DATA: theresult TYPE i.

  DATA: car1 TYPE REF TO car.

  CREATE OBJECT car1
    EXPORTING
      make     = 'AUDI'
      model    = 'A4'
      numseats = 5
      maxspeed = 120.

car1->setnumseats( 4 ).
car1->viewcar( ).
ULINE.

car1->setnumseats( 3 ).
car1->viewcar( ).
ULINE.

car1->gofaster( EXPORTING increment = 25 IMPORTING result = theresult ).
car1->viewcar( ).
WRITE: / 'The Result of GOFASTER is: ', theresult LEFT-JUSTIFIED.
ULINE.

car1->goslower( EXPORTING increment = 15 RECEIVING result = theresult ).
car1->viewcar( ).
WRITE: / 'The Result of GOSLOWER is: ', theresult LEFT-JUSTIFIED.
ULINE.

******************************************************
* Example of OPTIONAL parameter

METHODS numflightsto
    IMPORTING city TYPE s_to_city OPTIONAL
    RETURNING VALUE(numflight) TYPE i.
    
METHOD numflightsto.
    DATA citytest TYPE s_to_city.
    IF city IS INITIAL.
        citytest = 'JFK'.
    ELSE.
        citytest = city.
    ENDIF.
    LOOP AT flight_table TRANSPORTING NO FIELDS WHERE
                                    airpto = citytest.
        numflight = numflight + 1.
    ENDLOOP.
ENDMETHOD.

temp = my_flight_class->numflightsto( 'FRA' ).
temp = my_flight_class->numflightsto( ).