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

my first class

Former Member
0 Likes
693

Hello!

I have created a new class:

CALL METHOD me->say

receiving

say_me = a

.

write: a.

And abap says, "me" is not defined. How/where can I define it? I am a beginner....

Thanks!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
663

Hi,

you need to create reference of the class.

then you can use that refernce to call the method.

here 'me' is the reference variable.

to create reference variable..

ex:-

CREATE OBJECT me.

data: me type ref to <class name>.

call method me-><methodname>...

Regards,

venkat N

Hello!

I have created a new class:

CALL METHOD me->say

receiving

say_me = a

.

write: a.

And abap says, "me" is not defined. How/where can I define it? I am a beginner....

Thanks!

3 REPLIES 3
Read only

Former Member
0 Likes
663

Before implementing a class u need to define it.

See the demo program below.



REPORT demo_class_counter .

CLASS counter DEFINITION.
  PUBLIC SECTION.
    METHODS: set IMPORTING value(set_value) TYPE i,
             increment,
             get EXPORTING value(get_value) TYPE i.
  PRIVATE SECTION.
    DATA count TYPE i.
ENDCLASS.

CLASS counter IMPLEMENTATION.
  METHOD set.
    count = set_value.
  ENDMETHOD.
  METHOD increment.
    ADD 1 TO count.
  ENDMETHOD.
  METHOD get.
    get_value = count.
  ENDMETHOD.
ENDCLASS.

DATA number TYPE i VALUE 5.
DATA cnt TYPE REF TO counter.

START-OF-SELECTION.

  CREATE OBJECT cnt.

  CALL METHOD cnt->set EXPORTING set_value = number.

  DO 3 TIMES.
    CALL METHOD cnt->increment.
  ENDDO.

  CALL METHOD cnt->get IMPORTING get_value = number.

  WRITE number.

Regards,

Jagadish

Read only

Former Member
0 Likes
664

Hi,

you need to create reference of the class.

then you can use that refernce to call the method.

here 'me' is the reference variable.

to create reference variable..

ex:-

CREATE OBJECT me.

data: me type ref to <class name>.

call method me-><methodname>...

Regards,

venkat N

Read only

Former Member
0 Likes
663

Hi,

Please check the below...

A method can have a variable defined within it having the same name as one of the attributes of the class to which the method belongs to. To clearly identify the class level attribute, the selector ME is used. Check the below code...

class testclass definition.

public section.

data : i_num type i value 5.

methods : testmethod .

endclass.

class testclass implementation.

method :testmethod.

data : i_num type i value 2.

write:/5 me->i_num , "access variable of the class

/5 i_num . " access variable of the method

endmethod.

endclass.

start-of-selection.

data : i_num type i.

data : my_obj type ref to testclass.

create object : my_obj.

call method my_obj->testmethod.

Rgds,

Bujji