Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Type casting in ABAP

Former Member
0 Likes
4,049

Let's say I have the following code:

DATA idx TYPE i.

DATA msg TYPE string.

msg = "You are on index #".

CONCATENATE msg idx INTO msg.

WRITE msg.

This obviously won't work because idx isn't the right data type so my question is, how do I cast idx into a string-type? In C# you just did something like this:

(string)idx or idx.ToString()

4 REPLIES 4
Read only

former_member194669
Active Contributor
0 Likes
1,287

Hi,

May be this way.


DATA idx TYPE i.
DATA msg TYPE string.
DATA v_idxt(50) type c.

msg = 'You are on index #'.
write : idx to v_idxt.

CONCATENATE msg v_idxt INTO msg.

WRITE msg.

aRs

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,287

I'm not a big fan of moving something to another variable field simply because CONCATENATE can't handle it. I find it quite annoying so I would suggest doing the same like this instead.

DATA idx TYPE i.
DATA msg TYPE string.

* Simple move the value to the string field
msg = idx. 
condense idx no-gaps.

* Now add the literal text on the front.
CONCATENATE  'You are on index #' msg INTO msg.

WRITE msg.

Regards,

RIch Heilman

Read only

uwe_schieferstein
Active Contributor
0 Likes
1,287

Hello Steve

It may look like overkill but this static method is quite useful to convert pieces of data into a C-container (string or character field).

TYPES: BEGIN OF ty_s_msg.
TYPES:  idx           TYPE i.
TYPES:  blank(1)    TYPE c.
TYPES:  msg         TYPE bapi_msg.  " or TYPE string
TYPES: END OF ty_s_msg.
DATA:
  ls_msg    TYPE ty_s_msg.

  ls_msg-idx = '<index>'.
  ls_msg-blank = space.
  ls_msg-msg  = 'You are on index #'.

  CALL METHOD cl_abap_container_utilities=>fill_container_c
    EXPORTING
      ex_value = ls_msg
    IMPORTING
      ex_container = ls_msg-msg.  " concatenated values

Regards

Uwe

Read only

Former Member
0 Likes
1,287

Thanks to all! This is a great forum!