diff --git a/16_Data_Types_and_Objects.md b/16_Data_Types_and_Objects.md index 1132480..d359534 100644 --- a/16_Data_Types_and_Objects.md +++ b/16_Data_Types_and_Objects.md @@ -15,6 +15,7 @@ - [Data Objects](#data-objects) - [Declaring Data Objects](#declaring-data-objects) - [Assigning Values to Data Objects](#assigning-values-to-data-objects) + - [Conversion Rules when Assigning Elementary Data Objects](#conversion-rules-when-assigning-elementary-data-objects) - [Creating Data Objects Using Inline Declaration](#creating-data-objects-using-inline-declaration) - [Assigning References to Data Reference Variables](#assigning-references-to-data-reference-variables) - [Creating Anonymous Data Objects](#creating-anonymous-data-objects) @@ -763,6 +764,727 @@ str_a2 = some_itab[ 2 ]-carrname.
+#### Conversion Rules when Assigning Elementary Data Objects + +- The following code examples explore and comment on assignments of data objects with various elementary types. Conversion results are added as comments in the code. +- They focus on conversion rules when assigning an source to an target data object of different elementary types. +- Not all types or conversion options are covered, and some example assignments may seem nonsensical. +- For more information, see [Assignment and Conversion Rules](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconversion_rules.htm) in the ABAP Keyword Documentation. + +| Class include | Notes | +
| + +Global class + + | + ++ +``` abap +CLASS zcl_demo_abap DEFINITION + PUBLIC + FINAL + CREATE PUBLIC . + PUBLIC SECTION. - INTERFACES if_t100_dyn_msg. - ENDCLASS. - ``` -- In the global class, add the following code. - - A method declaration includes the local execption class following the `RAISING` addition. - - In a simple way, the example explores the raising of the exception. - - A message class of the executable example is used. Without the `MESSAGE` addition after `THROW`, a default message would be used. - - You can run the class using *F9*. - - ```abap - CLASS zcl_demo_abap DEFINITION - PUBLIC - FINAL - CREATE PUBLIC . - - PUBLIC SECTION. - INTERFACES if_oo_adt_classrun. - PROTECTED SECTION. - PRIVATE SECTION. - METHODS test_meth IMPORTING flag TYPE abap_boolean - RETURNING VALUE(hello) TYPE string - RAISING lcx_error. - ENDCLASS. + INTERFACES if_oo_adt_classrun. + PROTECTED SECTION. + PRIVATE SECTION. + METHODS raise_local_exception IMPORTING num TYPE i + RAISING lcx_error. +ENDCLASS. +CLASS zcl_demo_abap IMPLEMENTATION. - CLASS zcl_demo_abap IMPLEMENTATION. - - METHOD if_oo_adt_classrun~main. - + METHOD if_oo_adt_classrun~main. + DO 6 TIMES. TRY. - DATA(hi1) = test_meth( abap_true ). - out->write( hi1 ). - CATCH lcx_error INTO DATA(error1). - out->write( error1->get_text( ) ). + raise_local_exception( sy-index ). + CATCH lcx_error INTO DATA(error). + out->write( error->get_text( ) ). ENDTRY. + ENDDO. + ENDMETHOD. - TRY. - DATA(hi2) = test_meth( abap_false ). - out->write( hi2 ). - CATCH lcx_error INTO DATA(error2). - out->write( error2->get_text( ) ). - ENDTRY. + METHOD raise_local_exception. + CASE num. + WHEN 1. + RAISE EXCEPTION TYPE lcx_error EXPORTING textid = lcx_error=>lcx_error. + WHEN 2. + RAISE EXCEPTION TYPE lcx_error EXPORTING textid = lcx_error=>some_error text = `Some error text`. + WHEN 3. + RAISE EXCEPTION TYPE lcx_error MESSAGE e002(zdemo_abap_messages). + WHEN 4. + RAISE EXCEPTION TYPE lcx_error MESSAGE e005(zdemo_abap_messages) WITH 'Some' 'error' 'occurred'. + WHEN 5. + RAISE EXCEPTION TYPE lcx_error MESSAGE ID 'ZDEMO_ABAP_MESSAGES' TYPE 'E' NUMBER '005' WITH 'Hello' 'world'. + WHEN OTHERS. + "Default message + RAISE EXCEPTION TYPE lcx_error. + ENDCASE. + ENDMETHOD. - ENDMETHOD. +ENDCLASS. +``` + + | +
| + +Class-Relevant Local Types tab (CCDEF include) + + | + ++ +``` abap +CLASS lcx_error DEFINITION INHERITING FROM cx_static_check. + PUBLIC SECTION. + INTERFACES if_t100_dyn_msg. + + CONSTANTS: + BEGIN OF lcx_error, + msgid TYPE symsgid VALUE 'ZDEMO_ABAP_MESSAGES', + msgno TYPE symsgno VALUE '001', + attr1 TYPE scx_attrname VALUE '', + attr2 TYPE scx_attrname VALUE '', + attr3 TYPE scx_attrname VALUE '', + attr4 TYPE scx_attrname VALUE '', + END OF lcx_error. + + CONSTANTS: + BEGIN OF some_error, + msgid TYPE symsgid VALUE 'ZDEMO_ABAP_MESSAGES', + msgno TYPE symsgno VALUE '005', + attr1 TYPE scx_attrname VALUE 'TEXT', + attr2 TYPE scx_attrname VALUE '', + attr3 TYPE scx_attrname VALUE '', + attr4 TYPE scx_attrname VALUE '', + END OF some_error. + + DATA text TYPE string. + + METHODS constructor + IMPORTING + textid LIKE if_t100_message=>t100key OPTIONAL + previous LIKE previous OPTIONAL + text TYPE string OPTIONAL. + PROTECTED SECTION. + PRIVATE SECTION. +ENDCLASS. +``` + + | +
| + +Local Types tab (CCIMP include) + + | + ++ +``` abap +CLASS lcx_error IMPLEMENTATION. + METHOD constructor ##ADT_SUPPRESS_GENERATION. + super->constructor( previous = previous ). + + me->text = text. + CLEAR me->textid. + IF textid IS INITIAL. + if_t100_message~t100key = if_t100_message=>default_textid. + ELSE. + if_t100_message~t100key = textid. + ENDIF. + ENDMETHOD. +ENDCLASS. +``` + + | +