14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Calling Functions 2.4

sometimes the result of one routine has to have its type converted before it can be

passed into another routine (e.g., the period from a standard SAP ERP Financials

Financial Accounting function tends to be two characters long, but if you want to

pass that period into a Controlling functi on, then it needs to be three characters

long).

Another even more common example is that often you have a variable that is a

string, and you want to pass that variable into a function that only accepts input

of a certain type (say CHAR20). You cannot pass the variable directly; you have to

move it into a helper variable. This is shown in Listing 2.27.

DATA: ld_helper TYPE CHAR20,

ld_monster_name TYPE string.

ld_monster_name = ‘HUBERT’.

ld_helper = ld_monster_name.

lo_monster->invite_to_party( ld_helper ).

Listing 2.27 Moving a Variable into a Helper Variable

In ABAP 7.4, however, this can be simplified by use of a specific type of constructor

operator, CONV, the job of which is to convert values from one type to another.

In Listing 2.28, the CONV function reads the target data type from the IMPORTING

parameter definition and then converts the string into the type the parameter is

expecting. Once again, this is shorter, safer, and more adaptive.

DATA: ld_monster_name TYPE string.

ld_monster_name = ‘HUBERT’.

lo_monster->invite_to_party( CONV#( ld_monster_name ) ).

Listing 2.28 Converting a String with a Constructor Variable

2.4.4 Functions That Expect TYPE REF TO DATA

When you import parameters of functions or methods, a specific type of parameter

is usually required. However, in somesituations you do not know the variable

type until runtime. In such cases, the only way to achieve what you want is to use

dynamic programming.

In cases where you don’t know the exac t data type until runtime, often the

importing parameter of the method is typed as TYPE REF TO DATA . This is what

happens, for example, when you call methods that build up a dynamic signature

to pass into a dynamicall y defined method. You may ha ve also used parameters

101

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!