05-14-2020 8:26 AM
I don't know how to formulate the question properly, sorry in advance.
I have a basic LIST class, and a REPORT class, report->get_includes() returns TYPE REF TO LIST
When i try to compile the following code, i get the error saying i can't call a method from a reference
DATA(Y) = NEW REPORT( 'FOO' )->GET_INCLUDES( ).
DATA(Z) = Y->GET( 1 ).
When i change the return type of get_includes to TYPE LIST, i get the error saying "Y is not a reference variable"
So the errors say that i cannot call from a reference variable, but also have to call from a reference variable.
I hope i properly expressed myself, if i did not, i am happy to clarify.
Thanks in advance for any help!
05-14-2020 12:35 PM
Hi Former Member
Change the name of your class from LIST to ZLIST and it will work. The LIST is a keyword in ABAP that's why you get an error.
I would suggest using LCL_ class naming convention.
regards,
Mateusz
Edit: LIST is a data element, as noticed by sandra.rossi
Edit2: As rightly pointed by stephkoester the main issue is that class LIST was defined after the class REPORT. Having it defined before class REPORT would allow for code activation. That being said, having used class naming convention would trigger the correct syntax check error, which would point to the right issue.
05-14-2020 8:31 AM
Hi Former Member
Could you share a piece of the code, please?
regards,
Mateusz
05-14-2020 8:33 AM
This is a very generic error. The error can be one misplaced character. So in order to help you we definitely need the exact Code part. Type Definition and those two calls should be sufficient though.
05-14-2020 8:40 AM
mateuszadamus michael.biber2
Thanks for the reply, here are the definitions:
Report:
METHODS GET_INCLUDES RETURNING VALUE(INCLUDES) TYPE REF TO LIST.
List:
GET IMPORTING VALUE(IN) TYPE I RETURNING VALUE(OUT) TYPE REF TO DATA,
05-14-2020 8:57 AM
I don't know Kevin, it works for me. 😉
You have to give more information, preferably the code. Otherwise I'm not able to figure out what the issue is.
regards,
Mateusz
05-14-2020 12:28 PM
mateuszadamus i tried your code and it worked for me too, and everything seems to be the same in my code. Did i make one stupid mistake?
(Also, is there a better way to post source code? For small pieces, screenshots are great, but what about longer pieces of code? Also thanks a lot again for your help, i really appreciate it!)
CLASS REPORT DEFINITION.
PUBLIC SECTION.
METHODS: CONSTRUCTOR
IMPORTING VALUE(NAME) TYPE STRING,
GET_INCLUDES
RETURNING VALUE(INCLUDES) TYPE REF TO LIST.
PRIVATE SECTION.
DATA SOURCE TYPE STRING_TABLE.
ENDCLASS.
CLASS LIST DEFINITION.
PUBLIC SECTION.
METHODS: CONSTRUCTOR,
ADD
IMPORTING
VALUE(IN) TYPE REF TO DATA,
REMOVE
IMPORTING
VALUE(IN) TYPE REF TO DATA,
GET
IMPORTING
VALUE(IN) TYPE I
RETURNING
VALUE(OUT) TYPE REF TO DATA,
INDEXOF
IMPORTING
VALUE(IN) TYPE REF TO DATA
RETURNING
VALUE(OUT) TYPE I.
PRIVATE SECTION.
DATA CONTENT TYPE TABLE OF REF TO DATA.
ENDCLASS.
CLASS REPORT IMPLEMENTATION.
METHOD CONSTRUCTOR.
ENDMETHOD.
METHOD GET_INCLUDES.
ENDMETHOD.
ENDCLASS.
CLASS LIST IMPLEMENTATION.
METHOD CONSTRUCTOR.
ENDMETHOD.
METHOD ADD.
APPEND IN TO CONTENT.
ENDMETHOD.
METHOD REMOVE.
DELETE CONTENT WHERE TABLE_LINE EQ IN.
ENDMETHOD.
METHOD GET.
OUT = CONTENT[ IN ].
ENDMETHOD.
METHOD INDEXOF.
DATA(INDEX) = -1.
READ TABLE CONTENT WITH KEY TABLE_LINE = IN TRANSPORTING NO FIELDS.
IF SY-SUBRC = 0.
INDEX = SY-TABIX.
ENDIF.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(Y) = NEW REPORT( 'FOO' )->GET_INCLUDES( ).
DATA(Z) = Y->GET( 1 ).
05-14-2020 12:35 PM
Hi Former Member
Change the name of your class from LIST to ZLIST and it will work. The LIST is a keyword in ABAP that's why you get an error.
I would suggest using LCL_ class naming convention.
regards,
Mateusz
Edit: LIST is a data element, as noticed by sandra.rossi
Edit2: As rightly pointed by stephkoester the main issue is that class LIST was defined after the class REPORT. Having it defined before class REPORT would allow for code activation. That being said, having used class naming convention would trigger the correct syntax check error, which would point to the right issue.
05-17-2020 2:37 PM
This is not the correct answer, because you can use nearly every word for local objects/variables. Even keywords can be used like he used report as local class object.
The problem why the code couldn't get activated is, that the class definition of list needs to be before definition of class report.
Greetings
Stephan
05-14-2020 12:36 PM
Hi Former Member
Yes, definitely there is a better way to show code than pasting images. I was in a hurry...
There is a "Code" button on the toolbar of the message editor. This would be preferable, I guess.
regards,
Mateusz
05-14-2020 12:39 PM
Former Member Please post code with CODE button, like this:
CLASS report DEFINITION.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING VALUE(name) TYPE string,
get_includes
RETURNING VALUE(includes) TYPE REF TO list.
PRIVATE SECTION.
DATA source TYPE string_table.
ENDCLASS.
05-14-2020 12:43 PM
It's because LIST is a data element (1 character) and not a class.
You should prefix classes with LCL_ to avoid this kind of problem, and also place the declarations in the right order (in your case, the class LIST is defined after TYPE REF TO list, so SAP considers LIST as the data element).
05-14-2020 1:07 PM
Thanks sandra.rossi, that solved the problem! I had to define LIST first.
Also thanks to you and mateuszadamus for all the help on here, i am pretty new and dont know how this site works