Sunday, 9 March 2014

Changing the Value of the Private Attribute of a class Out Side of the Class

Scenario: This Post describes changing the Value of the Private attribute of the class Outside of the class by Using Reference Technique.
_________________________________________________________________________________
Step1. Create a Report in SE38 and execute it.

REPORT  ztest_private_attr_val_change.

*----------------------------------------------------------------------*
*       CLASS lcl_private_attr_change_tech DEFINITION
*----------------------------------------------------------------------*


CLASS lcl_private_attr_change_tech DEFINITION.
  PUBLIC SECTION.
    METHODS get_private_attribute_value,
                         get_refrence_of_priv_attr EXPORTING e_fp_salary TYPE REF TO data.

  PRIVATE SECTION.
    DATA lv_salary TYPE p  DECIMALS VALUE '50000.00'.
ENDCLASS.                    "lcl_private_attr_change_tech DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_private_attr_change_tech IMPLEMENTATION
*----------------------------------------------------------------------*


CLASS lcl_private_attr_change_tech IMPLEMENTATION.

  METHOD get_private_attribute_value.
    WRITE :'Value of Private Attribute Salary is :'lv_salary.
  ENDMETHOD.                    "get_private_attribute_value


  METHOD get_refrence_of_priv_attr.
    GET REFERENCE OF lv_salary INTO e_fp_salary.
  ENDMETHOD.                    "get_refrence_of_pri_attr


ENDCLASS.                    "lcl_private_attr_change_tech IMPLEMENTATION

START-OF-SELECTION.

  DATA lv_oref TYPE REF TO lcl_private_attr_change_tech.
  DATA lv_ref_salary TYPE REF TO data.
  FIELD-SYMBOLS <fs_salary> TYPE any.
  CREATE OBJECT lv_oref.
  CALL METHOD lv_oref->get_private_attribute_value.
******get the reference of the private attribute of the class*********
  CALL METHOD lv_oref->get_refrence_of_priv_attr
    IMPORTING
      e_fp_salary lv_ref_salary.

*******Assign the data reference to the Field Symbol*******
  ASSIGN lv_ref_salary->TO <fs_salary>.

*******Change the value of the Private attribute by the reference using the Field Symbol******


  <fs_salary> '80000.00' .

  CALL METHOD lv_oref->get_private_attribute_value.



_________________________________________________________________________________
Step2. Execute it and see the Output.










_________________________________________________________________________________

No comments:

Comments system

Disqus Shortname