Thursday 21 September 2017

ABAP: Defining Structured variables

In my previous post, I explained how to define variables (by using basic types of ABAP). We can also define complex variables by combining these primitive variables.

Employee has first_name, last_name, age, address. Following statements are used to created Employee structure.

TYPES: BEGIN OF Employee,
      first_name TYPE STRING,
      last_name TYPE STRING,
      age TYPE I,
      address TYPE STRING,
      END OF EMPLOYEE.

How to define a variable of type Employee?
It is just like defining normal variables.
DATA emp1 TYPE Employee.

Above statement creates a variable of type Employee.

How to access the variables inside emp1?
The fields of structured variables are accessed using ‘-‘.
emp1-first_name = 'Hari Krishna'.

Above statement sets the value ‘Hari Krishna’ to the field ‘first_name’ of the variable emp1.

Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*& Report Z_HELLO_WORLD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_HELLO_WORLD.

* Defining Variables
TYPES: BEGIN OF Employee,
      first_name TYPE STRING,
      last_name TYPE STRING,
      age TYPE I,
      address TYPE STRING,
      END OF EMPLOYEE.

DATA emp1 TYPE Employee.
emp1-first_name = 'Hari Krishna'.
emp1-last_name = 'Gurram'.
emp1-age = 28.
emp1-address = 'Bangalore'.

Write emp1-first_name.
Write / emp1-last_name.
Write / emp1-age.
Write / emp1-address.



Previous                                                 Next                                                 Home

No comments:

Post a Comment