Friday, September 9, 2016

Object-oriented Programming Syntaxs

Creating Data Objects Dynamically 

All of the data objects that you define in the declaration part of a program using statements such as DATA are created statically, and already exist when you start the program. To create a data object dynamically during a program, you need a data reference variable and the following statement:
CREATE DATA dref {TYPE type}|{LIKE dobj}.
This statement creates a data object in the internal session of the current ABAP program. After the statement, the data reference variable dref points to the object. The data object that you create does not have its own name. You can only address it using a data reference variable. To access the contents of the data object, you must dereference the data reference.
You must specify the data type of the object using the TYPE or LIKE addition. In contrast to the use of the TYPE addition in other ABAP statements, you can use the CREATE DATA addition to dynamically specify the data type as the contents of a field.
CREATE DATA dref TYPE (name).
Here, name is the name of a field that contains the name of the required data type.

Example:
When you use "type ref to data" you are mentioning the data type to be as generic. You make use of this kind of definition when you aren't sure about which kind of data you want to assign to the variable. If you want to read the values from this kind of data type you need to first cast it into a data type of your specific structure. 

Getting References to Data Objects 

The following statements allow you to place a data reference - which points to an existing data object - in a reference variable:

GET REFERENCE OF dobj INTO dref.


dobj can be a statically-declared data object, or a field symbol pointing to any object (including a dynamic object). If you specify a field symbol to which no objects have yet been assigned, a run-time error occurs.

dref is reference variable which contains the data reference of dobj. The data changes on reference variable dref will also change on dobj as well.

If you place a reference to a local field in a procedure in a global reference variable, the reference will become invalid when you leave the procedure. You cannot then dereference the reference variable.
REPORT demo_data_reference.
TYPES: BEGIN OF t_struct,
  col1 TYPE i,
  col2 TYPE i,
  END OF t_struct.
DATA: dref1 TYPE REF TO data,
 dref2 TYPE REF TO data.
FIELD-SYMBOLS: <fs1> TYPE t_struct,
  <fs2> TYPE i.
CREATE DATA dref1 TYPE t_struct.
ASSIGN dref1->* TO <fs1>.
<fs1>-col1 = 1.
<fs1>-col2 = 2.
dref2 = dref1.
ASSIGN dref2->* TO <fs2> CASTING.
WRITE / <fs2>.
GET REFERENCE OF <fs1>-col2 INTO dref2.
ASSIGN dref2->* TO <fs2>.
WRITE / <fs2>.
The list output is:
 1
 2

This example declares two data reference variables dref1 and dref2dref1 is used to create a structured dynamic data object. This is dereferenced with the field symbol <fs1>, and values are then assigned to it. dref1 is assigned to dref2dref2 then points to the structured data object. When dref2 is dereferenced, it is cast to the elementary type i of field symbol <fs2>. Its first component is assigned to the field symbol. GET REFERENCE is then used to get a reference to the second component not the structured data object in dref2. It is dereferenced without casting.

Casting Data Objects 

When you assign a data object to a field symbol, you can cast to any data type. This means that any area of memory can be viewed as having assumed a given type. You can then address parts of fields symbolically without having to use offset/length addressing.
A cast is performed using the CASTING addition of the ASSIGN statement. The CASTINGaddition allows you to assign a data object to a field symbol where the type of the data object is incompatible with that of the field symbol. There are two types of casting: casting with an implicit type declaration and casting with an explicit type declaration.
The CASTING addition replaces some obsolete additions of the ASSIGN statement that should no longer be used.

Casting with an Implicit Type Declaration

When field symbol and data object don't have the same type but similar. We can use the following statement to assign and cast the data object to field symbol:
ASSIGN ... TO <fs> CASTING.
When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the same type as the field symbol. The length and alignment of data object must be compatible with the field symbol type. Otherwise the system returns a run-time error. If the type of either the field symbol or the data object is – or contains – a string, reference type, or internal table, the type and position of these components must match exactly. Otherwise, a run-time error occurs.
Example
REPORT demo_field_symbols_casting.
TYPES: 
BEGIN OF t_date,
 year(4) TYPE n,
 month(2) TYPE n,
 day(2)  TYPE n,
  END OF t_date.
FIELD-SYMBOLS <fs> TYPE t_date.
ASSIGN sy-datum TO <fs> CASTING.
WRITE / sy-datum.
SKIP.
WRITE: / <fs>-year , / <fs>-month, / <fs>-day.
The output looks something like this:
1999/05/19
1999
05
19
In this example, the field symbol <fs> is fully typed using the local program type t_date. The sy-datum field can be treated like a structure as a result of the CASTING addition of the ASSIGNstatement. This would not be possible without the CASTING addition, since sy-datum is incompatible with the field symbol type.

Dereferencing Data References
To access the contents of the data object to which a data reference is pointing, you must dereference it - to make it not a reference by assigning to field symbol.
CREATE DATA dref TYPE ...
ASSIGN dref->* TO <fs> [CASTING ...].
This statement assigns the data object to the field symbol <fs> which the data reference in the reference variable <dref> points to. If the assignment is successful, sy-subrc is set to zero.
If the field symbol is fully generic, it adopts the data type of the data object. If the field symbol is partially or fully typed, the system checks the data types for compatibility. Casting is also possible for the assigned data object.
If the data reference in <dref> is initial or invalid, you cannot dereference it. The field symbol remains unchanged, and sy-subrc is set to 4. 
If you create a data object dynamically, the only way to access its contents is to use dereferencing.

Casting with an Explicit Type Declaration

If the field symbol is neither fully typed nor generically typed, use the following form of the ASSIGN statement:
ASSIGN ... TO <fs> CASTING {TYPE type}|{LIKE dobj} [DECIMALS dec].
When the system accesses the field symbol, the content of the assigned data object is interpreted as if it had the type declared in the statement. After the TYPE addition, you can declare the name of a data object enclosed in parentheses. The content of this data object indicates the data type at runtime.
You cannot enter a type unless an explicit cast to the data object is logical. For example, there is no point casting a standard table to a sorted table, or an object reference to a data reference. Thus, you cannot declare table types (such as SORTED TABLE) or reference types (using REF TO) after the TYPEaddition. If the data type contains implicit string, reference types, or internal tables, these components must also occur in the assigned data object with their type and position.. Otherwise, the system returns a syntax or runtime error.
The data type declared in the TYPE or LIKEaddition must be compatible with the generic type of the field symbol. The generic type can be left as it is or specialized. However, you cannot reset known technical attributes of the field symbol. For example, if the field symbol has the completely generic type ANY , you can declare another permitted generic type. If the field symbol has the generic type cnp, or x, you can only specify the length and the number of decimal places. You cannot make an explicit type declaration for a fully typed field symbol, since, if the field symbol is fully typed already, you cannot specialize it further. In any case, the system checks statically for this at runtime, where possible.
You should use the DECIMALS addition if the content of the assigned data object can be interpreted as a packed number. The field symbol is then given dec decimal places. DECIMALS can only be used if no type specification is made of if the type is specified after TYPE. If there is no type specification, type p is used implicitly. Moreover, you cannot use the LIKE and DECIMALS additions together.
Example
REPORT demo_field_symbols_casting_typ.
TYPES: BEGIN OF t_date,
 year(4) TYPE n,
 month(2) TYPE n,
 day(2)  TYPE n,
  END OF t_date.
FIELD-SYMBOLS: <fs> TYPE ANY,
  <f> TYPE n.
ASSIGN sy-datum TO <fs> CASTING TYPE t_date.
WRITE / sy-datum.
SKIP.
DO.
 ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <f>.

 IF sy-subrc <> 0.
 EXIT.

 ENDIF.
 WRITE / <f>.
ENDDO.
The output looks something like this:
1999/05/19
1999
05
19

In this example, the field symbol <fs> is completely generic. A cast is performed on the field sy-datum  to the local program type t_date using the CASTING addition of the ASSIGN statement. The field symbol <fs> can now be treated like a structure, but it does not know any components. Therefore, it must be assigned – component by component – to another field symbol <f>.

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...