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: 

ABAP OOPS

Former Member
0 Kudos

Hi all,

I am trying to learn OOP's concepts oin ABAP.I wrote a very small program but it gives me an error saying <b>"Statement not accessible"</b>. I am forwarding my whole code, please eplain me where I am going wrong.

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

REPORT zclass_demo.

----


  • CLASS C_COUNTER DEFINITION

----


CLASS c_counter DEFINITION.

PUBLIC SECTION.

METHODS: set_counter IMPORTING value(set_value) TYPE i,

increment_counter,

get_counter EXPORTING value(get_value) TYPE i.

PRIVATE SECTION.

DATA count TYPE i.

ENDCLASS. "C_COUNTER DEFINITION

----


  • CLASS C_COUNTER IMPLEMENTATION

----


CLASS c_counter IMPLEMENTATION.

METHOD set_counter.

count = set_value.

ENDMETHOD. "SET_COUNTER

METHOD increment_counter.

ADD 1 TO count.

ENDMETHOD. "INCREMENT_COUNTER

METHOD get_counter.

get_value = count.

ENDMETHOD. "GET_COUNTER

ENDCLASS. "C_COUNTER IMPLEMENTATION

*---- Data Statement

DATA cref1 TYPE REF TO c_counter.

<u><b>CREATE OBJECT cref1.</b></u>

DATA num TYPE i VALUE 5.

CALL METHOD cref1->set_counter

EXPORTING

set_value = number.

DO 3 TIMES.

CALL METHOD cfref1->increment_counter.

ENDDO.

CALL METHOD cref1->get_counter

IMPORTING

get_value = number.

WRITE number.

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

The problem is with the underlined line of the code.

Regards,

Varun.

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Its actually just a warning, but there are two ways to fix it. The first one is to insert the START-OF-SELECTION event .



*---- Data Statement

data cref1 type ref to c_counter.


<b>start-of-selection.</b>
  create object cref1.

  data number type i value 5.


  call method cref1->set_counter
  exporting
  set_value = number.

  do 3 times.
    call method cref1->increment_counter.
  enddo.

  call method cref1->get_counter
  importing
  get_value = number.

  write number.


The second is to put the implementation at the end of the program.





report zclass_demo.

*---------------------------------------------------------------------*
* CLASS C_COUNTER DEFINITION
*---------------------------------------------------------------------*
class c_counter definition.

  public section.
    methods: set_counter importing value(set_value) type i,
    increment_counter,
    get_counter exporting value(get_value) type i.

  private section.
    data count type i.

endclass. "C_COUNTER DEFINITION



*---- Data Statement

data cref1 type ref to c_counter.


create object cref1.

data number type i value 5.


call method cref1->set_counter
exporting
set_value = number.

do 3 times.
  call method cref1->increment_counter.
enddo.

call method cref1->get_counter
importing
get_value = number.

write number.



<b>*---------------------------------------------------------------------*
* CLASS C_COUNTER IMPLEMENTATION
*---------------------------------------------------------------------*
class c_counter implementation.

  method set_counter.
    count = set_value.
  endmethod. "SET_COUNTER

  method increment_counter.
    add 1 to count.
  endmethod. "INCREMENT_COUNTER

  method get_counter.
    get_value = count.
  endmethod. "GET_COUNTER

endclass. "C_COUNTER IMPLEMENTATION</b>

REgards,

Rich Heilman

9 REPLIES 9

Former Member
0 Kudos

Hi

Where have you defined NUMBER?

Anyway you have to insert the START-OF-SELECTION event

DATA cref1 TYPE REF TO c_counter.

DATA number TYPE i VALUE 5.

START-OF-SELECTION. <----


CREATE OBJECT cref1.

Max

0 Kudos

Hi Max,

Its defined under underlined code. I know I gave it wrong and I corrected it now. But it still gives me the same error.

Regards,

Varun.

0 Kudos

Varun, please see my above post.

Regards,

Rich Heilman

0 Kudos

please C my post

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Its actually just a warning, but there are two ways to fix it. The first one is to insert the START-OF-SELECTION event .



*---- Data Statement

data cref1 type ref to c_counter.


<b>start-of-selection.</b>
  create object cref1.

  data number type i value 5.


  call method cref1->set_counter
  exporting
  set_value = number.

  do 3 times.
    call method cref1->increment_counter.
  enddo.

  call method cref1->get_counter
  importing
  get_value = number.

  write number.


The second is to put the implementation at the end of the program.





report zclass_demo.

*---------------------------------------------------------------------*
* CLASS C_COUNTER DEFINITION
*---------------------------------------------------------------------*
class c_counter definition.

  public section.
    methods: set_counter importing value(set_value) type i,
    increment_counter,
    get_counter exporting value(get_value) type i.

  private section.
    data count type i.

endclass. "C_COUNTER DEFINITION



*---- Data Statement

data cref1 type ref to c_counter.


create object cref1.

data number type i value 5.


call method cref1->set_counter
exporting
set_value = number.

do 3 times.
  call method cref1->increment_counter.
enddo.

call method cref1->get_counter
importing
get_value = number.

write number.



<b>*---------------------------------------------------------------------*
* CLASS C_COUNTER IMPLEMENTATION
*---------------------------------------------------------------------*
class c_counter implementation.

  method set_counter.
    count = set_value.
  endmethod. "SET_COUNTER

  method increment_counter.
    add 1 to count.
  endmethod. "INCREMENT_COUNTER

  method get_counter.
    get_value = count.
  endmethod. "GET_COUNTER

endclass. "C_COUNTER IMPLEMENTATION</b>

REgards,

Rich Heilman

Former Member
0 Kudos

CREATE OBJECT cref1.

this should be after start-of-selection.

Former Member
0 Kudos

Try the following statement before the offending line.

start-of-selection.

Former Member
0 Kudos

Hi Varun,

Add this code "CLASS c_counter DEFINITION create public".

instaed of "CLASS c_counter DEFINITION " It should work.

Cheers

Ankur

Former Member
0 Kudos

HI

the problem is

<b>Report <name>

DATA cref1 TYPE REF TO c_counter.

Start-of-selection.</b>

CREATE OBJECT cref1.

DATA num TYPE i VALUE 5.

CALL METHOD cref1->set_counter

EXPORTING

set_value = number.

DO 3 TIMES.

CALL METHOD cfref1->increment_counter.

ENDDO.

CALL METHOD cref1->get_counter

IMPORTING

get_value = number.

WRITE number.

then add the implementation at the end

<b>the problem was the create object should be defined after the start of selection. that should the first line to be executed. that's why it gave the error statement not accessible</b>

regards

kishore

reward if helpful

Message was edited by: Harikishore Sreenivasulu