Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Is possible convert a form to a method?

Can I convert this form to a method?

The original perform is:

************************************************

DATA: num1 TYPE i,

num2 TYPE i,

sum TYPE i.

num1 = 2. num2 = 4.

PERFORM addit USING num1 num2 CHANGING sum.

num1 = 7. num2 = 11.

PERFORM addit USING num1 num2 CHANGING sum.

FORM addit

USING add_num1 TYPE any

add_num2 TYPE any

CHANGING add_sum TYPE any.

add_sum = add_num1 + add_num2.

PERFORM out USING add_num1 add_num2 add_sum.

ENDFORM.

FORM out

USING out_num1 TYPE any

out_num2 TYPE any

out_sum TYPE any.

WRITE: / 'Sum of', out_num1, 'and', out_num2, 'is', out_sum.

ENDFORM.

*************************************************

I convert the last code in oriented object:

*************************************************

REPORT zcu04_pruebaperform.

CLASS one DEFINITION.

PUBLIC SECTION.

METHODS: addit

IMPORTING

add_num1 TYPE any

add_num2 TYPE any

EXPORTING

add_sum TYPE any.

CLASS one IMPLEMENTATION.

method addit.

add_sum = add_num1 + add_num2.

ENDMETHOD.

ENDCLASS.

class two definition inheriting from one.

PUBLIC SECTION.

METHODS: out IMPORTING

out_num1 TYPE any

out_num2 TYPE any

out_sum TYPE any.

ENDCLASS.

CLASS two IMPLEMENTATION.

method out.

WRITE: / 'Sum of', out_num1, 'and', out_num2, 'is', out_sum.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA o_one TYPE REF TO one.

DATA o_two TYPE REF TO two.

CREATE OBJECT: o_one, o_two.

DATA: num1 TYPE i,

num2 TYPE i,

sum TYPE i.

num1 = 2. num2 = 4.

*when is called is used EXPORTING

CALL METHOD o_one->addit

EXPORTING

add_num1 = num1

add_num2 = num2

IMPORTING

add_sum = sum.

*PERFORM addit USING num1 num2 CHANGING sum.

CALL METHOD o_two->out

EXPORTING

out_num1 = num1

out_num2 = num2

out_sum = sum.

num1 = 7. num2 = 11.

CALL METHOD o_one->addit

EXPORTING

add_num1 = num1

add_num2 = num2

IMPORTING

add_sum = sum.

*PERFORM addit USING num1 num2 CHANGING sum.

CALL METHOD o_two->out

EXPORTING

out_num1 = num1

out_num2 = num2

out_sum = sum.

************************************************

But, How can I to call method in the class equal that the FORM, PERFORM to other FORM?

**FORM addit

    • USING add_num1 TYPE any

    • add_num2 TYPE any

    • CHANGING add_sum TYPE any.

**

    • add_sum = add_num1 + add_num2.

    • PERFORM out USING add_num1 add_num2 add_sum.

**

**ENDFORM.

...

...

CLASS one IMPLEMENTATION.

method addit.

add_sum = add_num1 + add_num2.

CALL METHOD o_two->out

EXPORTING

out_num1 = num1

out_num2 = num2

out_sum = sum.

ENDMETHOD.

ENDCLASS.

...

...

Error:

Field "O_TWO->OUT" is unknown. It is neither in one of the specified tables nor defined by a "DATA" statement.

Can you help me, please?

Former Member
replied

Sorry my friend, I've made a mistake.

The inherited method it's not redefined, so you can simple call:

me->out( )

Try this:

REPORT  yrobtest5.


*---------------------------------------------------------------------*
*       CLASS one DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS one DEFINITION.
  PUBLIC SECTION.
    METHODS: out
     IMPORTING
       out_num1 TYPE any
       out_num2 TYPE any
       out_sum TYPE any.

ENDCLASS.                    "one DEFINITION
*---------------------------------------------------------------------*
*       CLASS two DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS two DEFINITION INHERITING FROM one.
  PUBLIC SECTION.
    METHODS: addit
      IMPORTING
        add_num1 TYPE any
        add_num2 TYPE any
      EXPORTING
        add_sum  TYPE any.
ENDCLASS.                    "two DEFINITION
*---------------------------------------------------------------------*
*       CLASS one IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS one IMPLEMENTATION.
  METHOD out.
    WRITE: / 'Sum of', out_num1, 'and', out_num2, 'is', out_sum.
  ENDMETHOD.                    "out
ENDCLASS.                    "one IMPLEMENTATION
*---------------------------------------------------------------------*
*       CLASS two IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS two IMPLEMENTATION.
  METHOD addit.
    add_sum = add_num1 + add_num2.
    CALL METHOD me->out
      EXPORTING
        out_num1 = add_num1
        out_num2 = add_num2
        out_sum  = add_sum.
  ENDMETHOD.                    "addit
ENDCLASS.                    "two IMPLEMENTATION

START-OF-SELECTION.

  DATA o_one TYPE REF TO one.
  DATA o_two TYPE REF TO two.
  CREATE OBJECT: o_one, o_two.

  DATA: num1 TYPE i,
  num2 TYPE i,
  sum TYPE i.
  num1 = 2. num2 = 4.

*when is called is used EXPORTING
  CALL METHOD o_two->addit
    EXPORTING
      add_num1 = num1
      add_num2 = num2
    IMPORTING
      add_sum  = sum.

Hope this helps!

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question