ABAP Keyword Documentation → ABAP − Reference → Program Flow Logic → Control Structures → Branches
CASE TYPE OF
Other versions:
7.31 | 7.40 | 7.54
Syntax
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
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 of an interface. oref
is a
general expression position.
A class class
or an interface intf
valid in this
place 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 meet the condition.
Notes
-
A case distinction using
CASE TYPE OF
is a different spelling of the following branch usingIF
and the predicate expressionIS INSTANCE OF
and the corresponding rules and notes apply:
[statement_block1]
ELSEIF oref IS INSTANCE OF class|intf.
[statement_block2]
...
ELSE.
[statement_blockn]
ENDIF.
-
The control structure must specify more specific classes
class
or interfacesintf
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 meets the condition. oref
can be assigned to ref2
with static type
c2
without raising an exception.
CLASS c1 DEFINITION.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.
CLASS c3 DEFINITION INHERITING FROM c2.
ENDCLASS.
DATA oref TYPE REF TO object.
DATA: ref1 TYPE REF TO c1,
ref2 TYPE REF TO c2,
ref3 TYPE REF TO c3.
oref = NEW c2( ).
CASE TYPE OF oref.
WHEN TYPE c3.
ref3 ?= oref.
WHEN TYPE c2.
ref2 ?= oref.
WHEN TYPE c1.
ref1 ?= oref.
WHEN OTHERS.
...
ENDCASE.
Executable Example
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 existing object reference variable
ref
whose static type is more general or equal to the classclass
or interfaceintf
specified in the statement. -
An inline declaration
DATA(ref)
. In this case, an object reference variable with the static type of the classclass
or the interfaceintf
is declared.
If the addition INTO
is specified in the first WHEN
statement that meets the condition, the reference oref
is assigned to ref
before the statement block is executed. Here, both
up casts and down casts 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 preceding example.
CLASS c1 DEFINITION.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.
CLASS c3 DEFINITION INHERITING FROM c2.
ENDCLASS.
DATA oref TYPE REF TO object.
DATA: ref1 TYPE REF TO c1,
ref2 TYPE REF TO c2,
ref3 TYPE REF TO c3.
oref = NEW c2( ).
CASE TYPE OF oref.
WHEN TYPE c3 INTO ref3.
WHEN TYPE c2 INTO ref2.
WHEN TYPE c1 INTO ref1.
WHEN OTHERS.
...
ENDCASE.
Executable Example
Case Distinction CASE TYPE OF
for RTTI