09-29-2020 11:18 AM
Hi Guys,
I am new to ABAP OOPS , i wrote an sample code which need to display an employee details.
Here i need to display the method 'display_no_of_employees' ie., (if we display 2 emplyee 'Number of emplye = 2' and so on).
But i am unable to display the method as an output. what mistake as i doing?
Coding:
09-29-2020 1:18 PM
You will have litle clearing to do at first.
1. CLASS-METHODS <-- NO ! only for SingleTon design pattern
2. CLASS-DATA <-- Why ?!
3. CALL METHOD my_class->my-method <-- NO! my_class->my_method( ).
4. WRITE in CLASS <-- No, for test you could use CL_DEMO_OUTPUT (https://blogs.sap.com/2016/05/10/cldemooutput-part-1-of-2-usage/)
5. CREATE OBJECT my_object <-- No my_object = new #( ... ).
09-29-2020 1:28 PM
Currently you try to call your 'class-method' through an instance. Which works, but only if the object is also instantiated.
CALL METHOD g_employee3->display_no_of_employees.
The right way to call a class method would be to call it from the class lvl:
CALL METHOD lvl=>display_no_of_employees.
PS: I am not commenting on your style, just on the functionality you are trying to achieve.
09-29-2020 1:59 PM
michael.piesche, I am so disapointed, I thought you follow Clean Code Best Practice ...
😉
09-29-2020 7:26 PM
frdric.girod, you got me ;)
This time I just wanted to focus on the issue and tried not to overwhelm with other things that could be done better and stick with the users logic.
But you actually hit the nail on the head. Even though I have „studied“ the "Clean ABAP Cheat Sheet" as well as the styleguides from https://github.com/SAP/styleguides, my coding still is very influenced by the Hungarian notation.
I promise to do better in the future.
Thanks for pointing that out!
😉
09-29-2020 9:20 PM
09-30-2020 5:02 AM
matthew.billingham, you have been noticed too. I am happy to work with you as well ... well sort of work and somewhat with but still very well. 'Hats off.' 😉
09-30-2020 7:13 AM
Thanks for your quick response, i have acccess the method as below...
CALL METHOD g_employee2->display_no_of_employees. this is correct way.
---------------------------------------------------------------------------------------------------------------------------------
Another way as below (Method was in static need to access through both object as well class).
Yes, this way i got the O/P * CALL METHOD lvl=>display_no_of_employees*.
09-30-2020 8:50 AM
divsmart, so is your problem solved or can you be more specific on your issue? Can you show what is wrongly or not happening and what instead should be correctly happening?
>> But i am unable to display the method as an output. what mistake as i doing?
09-29-2020 1:30 PM
This is nothing to do with super classes. Nor ALV. Your question title should accurately reflect what your question is actually about.
09-30-2020 7:08 AM
frdric.girod
i now the concept of instance method as well static method using (constructor). Here i was struugged on output..
09-30-2020 7:13 AM