Ansicht
Dokumentation

ABAPCASE_TYPE - CASE TYPE

ABAPCASE_TYPE - CASE TYPE

BAL Application Log Documentation   ROGBILLS - Synchronize billing plans  
This documentation is copyright by SAP AG.
SAP E-Book

CASE TYPE OF

Short Reference



CASE TYPE OF oref.
  $[WHEN TYPE class$|intf $[INTO target1$].
    $[statement_block1$]$]
  $[WHEN TYPE class$|intf $[INTO target2$].
    $[statement_block2$]$]
  ...
  $[WHEN OTHERS.
    $[statement_blockn$]$]
ENDCASE.

Addition:

... INTO target

Effect

Special case distinction for object reference variables. This form of a control structure introduced using CASE checks the dynamic type of a non-initial object reference variable and the static type of an initial object reference variable oref. oref expects an object reference variable with the static type of a class or an interface. oref is a general expression position.

A class class or an interface intf that can be used at this position must be specified after WHEN TYPE. The first statement block statement_block is executed for which the class class or the interface intf is more general than or equal to the following:

  • A dynamic type of a non-initial object reference variable oref
  • A static type of an initial object reference variable oref

If this does not apply to any class class or interface intf, the statement block is executed after WHEN OTHERS. No object type class or intf can be specified if it is known statically that it does not fulfill the condition.

Notes

  • A case distinction using CASE TYPE OF is another way of writing the following branch using IF and the predicate expression IS INSTANCE OF and the corresponding rules and notes apply:
IF oref IS INSTANCE OF class$|intf.
  $[statement_block1$]
ELSEIF oref IS INSTANCE OF class$|intf.
  $[statement_block2$]
...
ELSE.
  $[statement_blockn$]
ENDIF.
  • In the control structure, more specific classes class or interfaces intf must be listed before more general classes or interfaces to enable the associated statement block to be executed.

Example

Case distinction for the dynamic type of an object reference variable oref, from more specific to more general classes. In the case shown, c2 is the first class that fulfills the condition. oref can be assigned to ref2 with static type c2 without raising an exception.

Case Distinction CASE TYPE OF for Exceptions

Addition

... INTO target

Effect

For every statement WHEN TYPE of a case distinction introduced using CASE TYPE OF, a target variable target can be specified after the optional addition INTO as follows:

  • An inline declaration DATA(ref) or FINAL(ref). In this case, an object reference variable with the static type of the class class or the interface intf is declared.

If the addition INTO is specified in the first WHEN statement that fulfills the condition, the reference oref is assigned to ref before the statement block is executed, whereby both upcasts and downcasts can be performed.

Note

The statement

WHEN TYPE class$|intf INTO ref.

is a semantically identical short form of

WHEN TYPE class$|intf.
  ref = oref.

The statement

WHEN TYPE class$|intf INTO DATA(ref).

is a semantically identical short form of

WHEN TYPE class$|intf.
  DATA(ref) = CAST class$|intf( oref ).


Example

The following case distinction shows the short form of the case distinction of the previous example.

Case Distinction CASE TYPE OF for RTTI






General Material Data   RFUMSV00 - Advance Return for Tax on Sales/Purchases  
This documentation is copyright by SAP AG.

Length: 6369 Date: 20240329 Time: 024202     sap01-206 ( 92 ms )