This in continuation to my first blog on SCN
Part I : Basics of OO ABAP Implementation
In this section we see the possibility of
- Passing parameter to our method.
- Various ways to receive values from methods
Well as we have already seen how to create a class and how to call methods I will directly jump in.
To pass value to a method probably there are 2 ways.
- Importing parameter of the class [Value is read-only cannot be changed in class].
- Changing parameter of the class [Value can be changed in class can be used as read write variable].
To receive value from a method probably there are 3 ways.
- Exporting parameter of the class
- Changing parameter of the class
- Returning parameter of the class [ This can be only one and the class cannot have Exporting / Changing parameter in this case].
You can explore the Exporting and Changing, the importing and returning are demonstrated below:
CLASS DEFINITION:
CLASS lcl_material_list DEFINITION FINAL.
PUBLIC SECTION.
METHODS: display_material
IMPORTING i_matnr TYPE matnr.
PRIVATE SECTION.
DATA : BEGIN OF ls_material,
werks TYPE werks_d,
lgort TYPE lgort_d,
labst TYPE labst,
END OF ls_material,
lt_material LIKE TABLE OF ls_material.
METHODS: get_description
IMPORTING i_matnr TYPE matnr
RETURNING VALUE(r_maktx) TYPE maktx.
ENDCLASS.
Now our class declaration is complete.
As you have noted we have stepped one step up, the method "get_description" is a private method and cannot be called from our program.
But it can be called within the class for internal usage only.
THE PROGRAM :
PARAMETERS : p_matnr TYPE matnr OBLIGATORY.
DATA : go_material TYPE REF TO lcl_material_list.
AT SELECTION-SCREEN.
SELECT SINGLE matnr FROM mara INTO p_matnr
WHERE matnr = p_matnr.
IF sy-subrc IS NOT INITIAL.
MESSAGE 'Invalid Material' TYPE 'I'.
ENDIF.
START-OF-SELECTION.
CREATE OBJECT go_material.
END-OF-SELECTION.
go_material->display_material( p_matnr ).
CLASS IMPLEMENTATION:
CLASS lcl_material_list IMPLEMENTATION.
METHOD get_description.
SELECT SINGLE maktx FROM makt
INTO r_maktx
WHERE matnr = i_matnr
AND spras = sy-langu.
ENDMETHOD.
METHOD display_material.
DATA : lv_maktx TYPE maktx.
lv_maktx = get_description( i_matnr ).
SELECT werks lgort labst FROM mard
INTO TABLE lt_material
WHERE matnr = i_matnr.
WRITE:/05 i_matnr,
25 lv_maktx.
ULINE.
WRITE:/05 'Plant' , 15 'SLoc' , 25 'Un-restricted Stock'.
ULINE.
LOOP AT lt_material INTO ls_material.
WRITE:/05 ls_material-werks,
15 ls_material-lgort,
25 ls_material-labst.
ENDLOOP.
ENDMETHOD.
ENDCLASS.