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.

6 comments:

Amar said...

Thank you for posting this..Its very useful.

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

sanayamohit 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!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

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

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

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

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!!!!!!

Keerthi Suresh said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-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!!!!!!