Application Development and Automation 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: 
Read only

calling a method

Former Member
0 Likes
825

hi

how can i call a method which is implemented in another class.

give me with example.

one more problem..

in my code .the following syntex is not working.

AT LINE-SELECTION.

CALL METHOD OBJ_OUT->LINESELECTION.

CALL METHOD OBJ_OUT->OUTPUT.

why and wat the problem...

plz answer ....

5 REPLIES 5
Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
719

Hi,

Check you aren't missing to create an instance of class:

Data OBJ_OUT type ref to <class>.

Check you aren't missing to instanciate this object:

CREATE OBJECT OBJ_OUT.

If the methods that you are trying to acess isn't public, you can't acess outside class.

Look at this example:



*---------------------------------------------------------------------*
*       CLASS main DEFINITION
*---------------------------------------------------------------------*
CLASS main DEFINITION.

" all component of PUBLIC SECTION are accessible 
" inside and outside class
  PUBLIC SECTION.
"   Instance Methods
    METHODS add_data IMPORTING i_data TYPE i.
    METHODS get_data RETURNING value(r_data) TYPE char20.
"   Static Methods
    CLASS-METHODS print IMPORTING value TYPE char20.

" all component of PRIVATE SECTION are accessible 
" only inside this class
  PRIVATE SECTION.

    DATA atribute TYPE char01.

ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS main IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS main IMPLEMENTATION.

  METHOD add_data.

    ADD i_data TO atribute.

  ENDMETHOD.

  METHOD get_data.

    CONCATENATE 'Atribute value' atribute
                               INTO r_data SEPARATED BY space.

  ENDMETHOD.

  METHOD print.

    WRITE value.

  ENDMETHOD.

ENDCLASS.

"Object reference of class MAIN
DATA: object_reference TYPE REF TO main.

DATA: var TYPE char20.

START-OF-SELECTION.

" Creating an instance of class MAIN in object_reference
  CREATE OBJECT object_reference.

" Calling an Instance Method of object "object_reference"
  CALL METHOD object_reference->add_data( i_data = 3 ).

  var = object_reference->get_data(  ).

" Calling a Static Method of class MAIN
  CALL METHOD main=>print EXPORTING value = var.

Regards.

Marcelo Ramos

Read only

0 Likes
719

hi

i understood which u mention ..

but my requirement is , the method1 present in class 1 is to accessed in the method2 in class 2...

plz reply for it.

Read only

0 Likes
719

to access methods of another class use inheritance concept.

define class2 inheriting from class1

Read only

0 Likes
719

Hi Lim,

To call a static method

call method cl_class_name=>method_name( ).

To call an instance method.

data: instance_name type ref to cl_class_name.

create object instance_name.

instance_name->method_name( ).

Of course in all these examples I have not passed any parameters.

Don't forget the F1 key is context-sensitive help.

Cheers

Graham

Read only

marcelo_ramos1
SAP Mentor
SAP Mentor
0 Likes
719

Hi,

Try this way:



*---------------------------------------------------------------------*
*       CLASS main DEFINITION
*---------------------------------------------------------------------*
CLASS main DEFINITION.

  PUBLIC SECTION.

    METHODS add_data IMPORTING i_data TYPE i.

    METHODS get_data RETURNING value(r_data) TYPE char20.

  PRIVATE SECTION.

    DATA atribute TYPE char01.

ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS main IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS main IMPLEMENTATION.

  METHOD add_data.

    ADD i_data TO atribute.

  ENDMETHOD.

  METHOD get_data.

    CONCATENATE 'Atribute value' atribute
                               INTO r_data SEPARATED BY space.

  ENDMETHOD.

ENDCLASS.


"2nd class

*---------------------------------------------------------------------*
*       CLASS print DEFINITION
*---------------------------------------------------------------------*
CLASS print DEFINITION.

  PUBLIC SECTION.

    DATA: object_reference TYPE REF TO main.

    DATA: var TYPE char20.

    METHODS print IMPORTING value TYPE i.

ENDCLASS.


*---------------------------------------------------------------------*
*       CLASS print IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS print IMPLEMENTATION.

  METHOD print.

    CREATE OBJECT object_reference.

"   Calling a methods from an class on other class
    CALL METHOD object_reference->add_data( i_data = value ).

"   Calling a methods from an class on other class
    var = object_reference->get_data(  ).

    WRITE var.

  ENDMETHOD.

ENDCLASS.

"-------

DATA: object_reference TYPE REF TO print.

START-OF-SELECTION.

  CREATE OBJECT object_reference.

  CALL METHOD object_reference->print EXPORTING value = 3.

Regards.

Marcelo Ramos