Monday, March 18, 2013

Preferred Parameter in a Method


Preferred parameter is used when we have more than one optional parameter. In this case we can declare the values to the parameters. But if we don’t mention the parameter then preferred parameter always populates the declared value.

In the following example we have a method with optional parameters v_opt & v_pre. We have mentioned that v_pre is the preferred parameter. Now at the calling of the method we are passing values to the parameters. At the first call we pass values to the mentioned parameters. So the system will easily recognize the variable’s values. But in the second call we haven’t mentioned the parameter. Since v_pre is the preferred parameter, it will populate with the declared values.

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_pr_para DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_pr_para DEFINITION.
  PUBLIC SECTION.
    METHODS m_pr_para IMPORTING v_opt TYPE i OPTIONAL
                                v_pre TYPE i OPTIONAL
                                PREFERRED PARAMETER v_pre.
ENDCLASS.                    "cl_pr_para DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_pr_para IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_pr_para IMPLEMENTATION.
  METHOD m_pr_para.
    WRITE: / 'Optional Parameter =',  v_opt,
           / 'Preferred Parameter =', v_pre.
  ENDMETHOD.                    "m_pr_para
ENDCLASS.                    "cl_pr_para IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO cl_pr_para.
  CREATE OBJECT obj.

  CALL METHOD obj->m_pr_para( v_opt = 10
                              v_pre = 12 ).
  SKIP.
  CALL METHOD obj->m_pr_para( 15 ).

We have the following output of this program.

3 comments:

Amar said...

Thank you for posting this..Its very useful.

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Unknown said...



Best SAP Success Factors Training Institute in Chennai
http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
Best SAP MM Training in Chennai
http://thecreatingexperts.com/sap-mm-training-in-chennai/
Best SAP SD Training in Chennai
http://thecreatingexperts.com/sap-sd-training-in-chennai/
http://thecreatingexperts.com/sap-hr-training-in-chennai/
Best SAP FICO Training in Chennai
http://thecreatingexperts.com/sap-fico-training-in-chennai/
Best SAP ABAP Training in Chennai
http://thecreatingexperts.com/sap-abap-training-in-chennai/
Best SAP BASIS Training in Chennai
http://thecreatingexperts.com/sap-basis-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!