Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Example program for returninng and importing with value addition

Former Member
0 Likes
2,214

HI ,

I want few example programs on how to use the abap oops with returning addition and importing with value addition as Im getting syntax error for the Program when Im declaring them with these additions

Thnaks .

4 REPLIES 4
Read only

Former Member
0 Likes
1,223

Hi,

To get some values from a method , one can use the EXPORTING, CHANGING or RETURNING parameters.

If one uses RETURNING parameters, the following restrictions apply:-

(1) No EXPORTING/CHANGING parameters can be used for the method.

(2) Only one RETURNING parameter can be used.

(3) RETURNING parameters are only passed by value.

The following program demonstrates the use of RETURNING parameters and the various ways to call a method with RETURNING parameter to get the value into some variable.

report ysubdel1 message-id 00.

data : w_num type i.

class c1 definition .

public section.

methods : m1 importing input1 type i

input2 type i

returning value(result) type i .

endclass.

class c1 implementation.

method : m1.

result = input1 * 2 + input2.

endmethod.

endclass.

start-of-selection.

data : obj1 type ref to c1 .

create object obj1.

  • Syntax 1

call method obj1->m1 EXPORTING input1 = 5

input2 = 4

RECEIVING result = w_num.

write:/5 w_num .

  • Syntax 2

w_num = obj1->m1( input1 = 10 input2 = 20 ).

write:/5 w_num .

  • Syntax 3

move obj1->m1( input1 = 2 input2 = 3 ) to w_num .

write:/5 w_num .

Output 14

40

7

Reward if useful.

Regards,

Swetha.

Read only

Former Member
0 Likes
1,223

Hello,

This statement declares a general instance method meth. Use additions ABSTRACT and FINAL to make the method abstract or final.

The additions IMPORTING, EXPORTING and CHANGING define the parameter interface of the method. After every addition, the corresponding formal parameters are defined by a specification of the list parameters.

The other additions determine which exceptions the method can propagate or trigger and determine whether the method is abstract or final.

Note

Within a method, you can use the logical expression IS SUPPLIED to check whether an actual parameter was assigned to an optional formal parameter at the call.

Addition 1

... IMPORTING parameters PREFERRED PARAMETER p

Effect

IMPORTING defines input parameters. When calling the method, you need not specify an appropriate actual parameter for every non-optional input parameter. During the call, the content of the actual parameter is passed to the input parameter. The content of the input parameter - for which the reference transfer is defined - cannot be changed in the method.

Use PREFERRED PARAMETER to identify an input parameter p1 p2 ... of list parameters after IMPORTING as a preferred parameter. This specification makes sense only if all input parameters are optional. When calling the method with the syntax

CALL METHOD meth( a ).

the actual parameter a is assigned to the preferred parameter if you have appropriate use of a functional method at an operand position.

Addition 2

... EXPORTING parameters

Effect

EXPORTING defines output parameters. When calling the method, you can specify an appropriate actual parameter for every output parameter. The content of the output parameter - which is defined for value transfer - is passed to the actual parameter at the call after the method has been completed successfully.

Note

An output parameter that is defined for the reference transfer is not initialized when the method is called. Therefore, no read access to it should take place before the first write access.

Addition 3

... CHANGING parameters

Effect

CHANGING defines input/output parameters. When calling the method, you must specify an appropriate actual parameter for every non-optional input/output parameter. The content of the actual parameter is passed to the input/output parameter at the call, and after the method has been completed, the content of the input/output parameter is passed to the actual parameter.

Example

The method read_spfli_into_table of this example has an input and an output parameter, which are typed fully by reference to the ABAP Dictionary.

CLASS flights DEFINITION.

PUBLIC SECTION.

METHODS read_spfli_into_table

IMPORTING VALUE(id) TYPE spfli-carrid

EXPORTING flight_tab TYPE spfli_tab.

...

ENDCLASS.

Addition 4

... RAISING exc1 exc2 ...

Effect

Use addition RAISING to declare the class-based exceptions exc1 exc2 ... that can be propagated from the method to the caller.

For exc1 exc2 ..., you can specify all exception classes that are visible at this position and are subclasses of CX_STATIC_CHECK or CX_DYNAMIC_CHECK. You must specify the exception classes in ascending order corresponding to their inheritance hierarchy.

Exceptions of the categories CX_STATIC_CHECK and CX_DYNAMIC_CHECK must be declared explicitly, otherwise a propagation results in a violation of the interface. An interface violation results in a treatable exception CX_SY_NO_HANDLER. Exceptions of category CX_NO_CHECK are always implicitly declared.

Notes

The declaration of exceptions of category CX_STATIC_CHECK is checked statically at the syntax check. For exceptions of category CX_DYNAMIC_CHECK, the check is executed at runtime.

In a method in which class-based exceptions are declared with the addition RAISING, you cannot use the statement CATCH SYSTEM-EXCEPTIONS. Instead, handle the relevant treatable exceptions in a TRY control structure.

Example

In class math, you can propagate all exceptions represented by class CX_SY_ARITHMETIC_ERROR and its subclasses from within method divide_1_by. If, for example, the input parameter operand is filled at the call with the value 0, then the exception CX_SY_ZERODIVIDE is triggered, propagated, and can, as shown in the example, be handled by the caller in a TRY control structure.

CLASS math DEFINITION.

PUBLIC SECTION.

METHODS divide_1_by

IMPORTING operand TYPE I

EXPORTING result TYPE f

RAISING cx_sy_arithmetic_error.

ENDCLASS.

CLASS math IMPLEMENTATION.

METHOD divide_1_by.

result = 1 / operand.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA oref TYPE REF TO math.

DATA exc TYPE REF TO cx_sy_arithmetic_error.

DATA res TYPE f.

DATA text TYPE string.

CREATE OBJECT oref.

TRY.

oref->divide_1_by( EXPORTING operand = 4

IMPORTING result = res ).

text = res.

CATCH cx_sy_arithmetic_error INTO exc.

text = exc->get_text( ).

ENDTRY.

MESSAGE text TYPE 'I'.

Addition 5

... EXCEPTIONS exc1 exc2 ...

Effect

Use addition EXCEPTIONS to define a list of non-class-based exceptions exc1 exc2..., which can be triggered with the statements RAISE or MESSAGE RAISING in the method. You specify identifiers exc1 exc2 ... for the exceptions to be defined at will and directly. Exceptions defined in this way are bound to the method - similar to formal parameters - and cannot be propagated.

If such an exception is triggered in a method and no return value has been assigned to it in the addition EXCEPTIONS of the CALL METHOD statement in the method call, then a runtime error occurs.

Note

The additions RAISING and EXCEPTIONS cannot be used simultaneously. For new developments starting at release 6.10, we recommend to use class-based exceptions, which are independent of the respective method.

Example

In the class math, for method divide_1_by an exception arith_error is defined, which is triggered in the method with the RAISE statement if an arithmetic error occurs. If, for example, the input parameter operand is filled with value 0 at the call, the exception arith_error is triggered in the method-internal handling of exception CX_SY_ZERODIVIDE and handled after the call of the method by evaluating sy-subrc.

CLASS math DEFINITION.

PUBLIC SECTION.

METHODS divide_1_by

IMPORTING operand TYPE I

EXPORTING result TYPE f

EXCEPTIONS arith_error.

ENDCLASS.

CLASS math IMPLEMENTATION.

METHOD divide_1_by.

TRY.

result = 1 / operand.

CATCH cx_sy_arithmetic_error.

RAISE arith_error.

ENDTRY.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA res TYPE f.

DATA oref TYPE REF TO math.

CREATE OBJECT oref.

oref->divide_1_by( EXPORTING operand = 4

IMPORTING result = res

EXCEPTIONS arith_error = 4 ).

IF sy-subrc = 0.

WRITE res.

ELSE.

WRITE 'Arithmetic error!'.

ENDIF.

Regards.

Read only

Former Member
0 Likes
1,223

Hi,

I hope the below info helps:

Parameters can be passed in three ways:

{ VALUE(p1) | REFERENCE(p1) | p1 }

Use VALUE or REFERENCE to determine whether a parameter p1 is passed by value or by reference. If only one name p1 is specified, the parameter is by default passed by reference. An input parameter passed by reference must not be changed in the method.

Read only

Former Member
0 Likes
1,223

Hi, refer to these examples

DEMO_ABAP_OBJECTS_GENERAL,DEMO_ABAP_OBJECTS_METHODS,DEMO_ABAP_OBJECTS