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.

Declaring and Creating Variables 2.2

you’ll use

lo_monster = NEW zcl_monster( name = ‘FRED’ ).

and instead of

DATA: ld_sanity TYPE zde_monster_sanity.

ld_sanity = 0.

you’ll use

ld_sanity = NEW zde_monster_sanity( 0 ).

As can be seen, this halves the number of lines of code you need. More importantly,

the debate about whether you declare the variables at the start of a routine

(per the official ABAP guidelines) or ju st before the variable (in order to aid

humans who might be reading the code) is no longer relevant. You have the type

of the variable right in your face at the instant the variable is created.

2.2.4 Filling Structures and Internal Tables while

Creating Them Using VALUE

No doubt, you are familiar with the common statement

DATA: ld_monster_name TYPE string VALUE ‘FRED’.

In this statement, you create a variable and give it an initial value, which can then

later be changed. So far, the ability to change initial values has only been available

for elementary data types. However, as of 7.4 the VALUE statement has come of

age, and you can do the same for structures and internal tables.

Listing 2.12 contains a pre-7.4 example of querying a database. In this example,

you create a selection table to be used inan SQL query, but you’re only interested

in laboratories where monsters will be created. However, because you can’t use

the VALUE statement, you have to fill up one or more work areas, and then append

them to the selection table.

DATA: lr_plant_type TYPE

RANGE OF zsmm_plant_master-plant_type,

ls_plant_type LIKE LINE OF lr_plant_type.

ls_plant_type-option = 'EQ'.

ls_plant_type-sign = 'I'.

ls_plant_type-low = 'L'. "Laboratory

APPEND ls_plant_type TO lr_plant_type.

lt_plants = lo_plant_query( lr_plant_type ).

Listing 2.12 Database Query without VALUE

93

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

Saved successfully!

Ooh no, something went wrong!