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

how to reference to my class attribute

former_member582701
Contributor
0 Likes
1,729

Hi,

In a method, if i receive for parameters a variable which it has the same name that my attribute name, how i can difference them?

Ex:

attribute = num_files.

importing num_files.

method Add_file.

num_files = num_files.

endmethod.

I have tried with ME, but it does not work.

Does exist any way to do this? (it is like sentence this-> in C++)

Thank you.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,370

Hi

PARAMETERS p_num TYPE i.

*---------------------------------------------------------------------*
*       CLASS lcl_my_class  DEFINTION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS lcl_my_class DEFINITION.
  PUBLIC SECTION.
    METHODS: my_method
                EXPORTING num_file TYPE i.

    DATA: num_file TYPE i.
ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS lcl_my_class IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS lcl_my_class IMPLEMENTATION.
  METHOD my_method.
    me->num_file = num_file.
  ENDMETHOD.
ENDCLASS.

DATA my_object TYPE REF TO lcl_my_class.

START-OF-SELECTION.

  create object my_object.

  CALL METHOD my_object->my_method IMPORTING num_file = p_num.

  WRITE my_object->num_file.

Max

Hi,

In a method, if i receive for parameters a variable which it has the same name that my attribute name, how i can difference them?

Ex:

attribute = num_files.

importing num_files.

method Add_file.

num_files = num_files.

endmethod.

I have tried with ME, but it does not work.

Does exist any way to do this? (it is like sentence this-> in C++)

Thank you.

4 REPLIES 4
Read only

Former Member
0 Likes
1,371

Hi

PARAMETERS p_num TYPE i.

*---------------------------------------------------------------------*
*       CLASS lcl_my_class  DEFINTION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS lcl_my_class DEFINITION.
  PUBLIC SECTION.
    METHODS: my_method
                EXPORTING num_file TYPE i.

    DATA: num_file TYPE i.
ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS lcl_my_class IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS lcl_my_class IMPLEMENTATION.
  METHOD my_method.
    me->num_file = num_file.
  ENDMETHOD.
ENDCLASS.

DATA my_object TYPE REF TO lcl_my_class.

START-OF-SELECTION.

  create object my_object.

  CALL METHOD my_object->my_method IMPORTING num_file = p_num.

  WRITE my_object->num_file.

Max

Read only

former_member69765
Contributor
0 Likes
1,370

The OWN attributes can always be referenced with: me->

me always stores the self reference in any object.

Cheers.

Read only

matt
Active Contributor
0 Likes
1,370

>

...

> I have tried with ME, but it does not work....

The other answers are correct for instance attributes - you use me. What do you mean by "it doesn't work". Do you get a syntax error? if so, what is it.

If the attribute is static,t hen use CLASSNAME=>ATTRIBUTE.

Tip: don't use the same name for parameters as attributes. I always prefix my importing parameters with i_, exporting with e_, changing with x_, and returning with r_.

matt

Read only

mauriciolauffer
Product and Topic Expert
Product and Topic Expert
0 Likes
1,370

Read the book: "Official ABAP Programming Guidelines". There is a charpter talking about this.

http://www.sap-press.com/products/Official-ABAP-Programming-Guidelines.html