Skip to Content
avatar image
Former Member

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?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Dec 04, 2007 at 09:00 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

    • 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!

  • avatar image
    Former Member
    Dec 04, 2007 at 03:04 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Dec 04, 2007 at 02:56 PM

    Try

    call method me->out

    Add comment
    10|10000 characters needed characters exceeded