Application Development 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: 

Is possible convert a form to a method?

Former Member
0 Kudos

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?

1 ACCEPTED SOLUTION

former_member199581
Active Participant
0 Kudos

By the principle of Inheritance, you can't call a method in a superclass that is defined in the subclass.

In method ADDIT of the Class One, the method OUT of the Class Two is not known by the framework.

Try to reverse the implementation. Define method OUT in the Superclass and the method ADDIT in the subclass.

Then, try to call method OUT like this:

super->out EXPORTING ...

Hope this helps,

R.

7 REPLIES 7

former_member195698
Active Contributor
0 Kudos

Try

call method me->out

Former Member
0 Kudos

Hi

Your program works fine you need only to write all DEFINITION and then all IMPLEMENTATION:

*---------------------------------------------------------------------*
*       CLASS one DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS ONE DEFINITION.
  PUBLIC SECTION.
    METHODS: ADDIT IMPORTING ADD_NUM1 TYPE ANY
                             ADD_NUM2 TYPE ANY
                   EXPORTING ADD_SUM TYPE ANY.
ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS two DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
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 one IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS ONE IMPLEMENTATION.
  METHOD ADDIT.
    ADD_SUM = ADD_NUM1 + ADD_NUM2.
  ENDMETHOD.
ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS two IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS TWO IMPLEMENTATION.
  METHOD OUT.
    WRITE: / 'Sum of', OUT_NUM1, 'and', OUT_NUM2, 'is', OUT_SUM.
  ENDMETHOD.

ENDCLASS.

DATA O_ONE TYPE REF TO ONE.
DATA O_TWO TYPE REF TO TWO.

DATA: NUM1 TYPE I,
      NUM2 TYPE I,
      SUM  TYPE I.


START-OF-SELECTION.

  CREATE OBJECT: O_ONE, O_TWO.


  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.

Max

former_member199581
Active Participant
0 Kudos

By the principle of Inheritance, you can't call a method in a superclass that is defined in the subclass.

In method ADDIT of the Class One, the method OUT of the Class Two is not known by the framework.

Try to reverse the implementation. Define method OUT in the Superclass and the method ADDIT in the subclass.

Then, try to call method OUT like this:

super->out EXPORTING ...

Hope this helps,

R.

0 Kudos

thank you for your kindness.

Robert, I try do it, but show this error:

<b>"You cannot use the statement "CALL METHOD" in the current environment.

However, you could use the similar statement "METHOD"."</b>

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 inheriting from one.
  public section.
    methods: addit
      importing
        add_num1 type any
        add_num2 type any
      exporting
        add_sum type any. 
 endclass.
class one implementation.
  method out.
    write: / 'Sum of', out_num1, 'and', out_num2, 'is', out_sum.
  endmethod.                    "out
endclass.                    "one IMPLEMENTATION
class two implementation.
  method addit.
    add_sum = add_num1 + add_num2.
  endmethod.                 "addit
    CALL METHOD super->out
      EXPORTING
        out_num1 = add_num1
        out_num2 = add_num2
        out_sum  = add_sum.   
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.

...

...

Cordial greetings.

0 Kudos

You made a syntax mistake my friend:

the statement ENDMETHOD is BEFORE than the statement CALL METHOD!

class two implementation.
  method addit.
    add_sum = add_num1 + add_num2.
  endmethod.    "addit    -------> THIS GOES TO THE END, BEFORE ENDCLASS
    CALL METHOD super->out
      EXPORTING
        out_num1 = add_num1
        out_num2 = add_num2
        out_sum  = add_sum.   
endclass.                    "two IMPLEMENTATION

Try again!

0 Kudos

Hi Robert, thanks, but I change the code and I try to do it, but now it says the next error syntaxis:

"You can only use SUPER-> to call previous implementations of the class' own methods (SUPER->ADDIT)"



I don´t understand;SUPER->ADDIT?, but ADDIT is inside of the class two ¿no?

Excuse me and again thanks.

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 inheriting from one.
  public section.
    methods: addit
      importing
        add_num1 type any
        add_num2 type any
      exporting
        add_sum type any. 
 endclass.
class one implementation.
  method out.
    write: / 'Sum of', out_num1, 'and', out_num2, 'is', out_sum.
  endmethod.                    "out
endclass.                    "one IMPLEMENTATION
class two implementation.
  method addit.
    add_sum = add_num1 + add_num2.
    CALL METHOD <b>super->out</b>
      EXPORTING
        out_num1 = add_num1
        out_num2 = add_num2
        out_sum  = add_sum.
  <b>endmethod.</b>
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.

0 Kudos

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!