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

How to determine type and access of REF TO DATA

Former Member
2,196

Hello experts,

I have a structure with two fields, one string as name/ID and the second one REF TO DATA as placeholder for any value, so DATA can take for example another string or a numeric value or whatever. Ok, now I want to handle this DATA based on it's type, which is unknown at that point. So I first DESCRIBE the field (its type). If for example this type results in a string, I want to move this string data from REF TO DATA into a local string value. Then I want to reformat the string value and save it again to the REF TO DATA field.

So, in short, I have these tasks:

1. determine type of REF TO DATA

2a. if it's a string, then move the val. of the string into a local string variable

2b. or alternatively do something else, if it's not a string value

3. do something with the local string, then save it back to the REF TO DATA field

Any ideas?

Thank in advance for your help!

Kind regards, Matthias

1 ACCEPTED SOLUTION
Read only

Former Member
0 Kudos
1,867

Hi,

U can use field symbols for doing this.It will help you solve this problem.

regards

Karthik.R

7 REPLIES 7
Read only

Former Member
0 Kudos
1,868

Hi,

U can use field symbols for doing this.It will help you solve this problem.

regards

Karthik.R

Read only

0 Kudos
1,867

Sorry, I don't understand how field symbols could solve my problem.

Can you please be a bit more specific, maybe with some sample code?

Read only

0 Kudos
1,867

Take a look at this piece of code. I think it's quite easy to understand how this works.


REPORT  ztest.

DATA: data TYPE REF TO data,
      string TYPE string,
      i      TYPE i.

FIELD-SYMBOLS: <FS>.


TYPES: BEGIN OF tipo,
        string TYPE string,
        data  TYPE REF TO data.
TYPES: END OF tipo.

DATA: var TYPE tipo.

VAR-STRING = 'HOLA'.
STRING = 'ADIOS'.
GET REFERENCE OF STRING INTO DATA.
VAR-DATA = DATA.


*VAR-STRING = 'HOLA'.
*I = 12.
*GET REFERENCE OF I INTO DATA.
*VAR-DATA = DATA.


ASSIGN VAR-DATA->* TO <FS>.


DATA: peo_structdescr TYPE REF TO cl_abap_typedescr.
peo_structdescr ?= cl_abap_datadescr=>describe_by_data( <FS> ).
CASE peo_structdescr->type_kind.
  WHEN 'I'.

  WHEN 'C'.

  WHEN 'g'.

ENDCASE.

Read only

0 Kudos
1,867

Hello Jose,

that does not seem to work, as the following code gives me 'l' as output (where it should be 'g'):


  DATA lr_typedesc  TYPE REF TO cl_abap_typedescr.
  DATA ls_rec       TYPE zats_ano_field_name_value.

  FIELD-SYMBOLS <fs> TYPE any.

  GET REFERENCE OF 'test' INTO ls_rec-value.
  ASSIGN ls_rec-value TO <fs>.
  lr_typedesc ?= cl_abap_datadescr=>describe_by_data( <fs> ).
  WRITE: / lr_typedesc->type_kind.

Any ideas?

Read only

0 Kudos
1,867

Hi Matthias

I think you missed a little and very important detail here:


  ASSIGN ls_rec-value TO <fs>.

That's not what we want, instead you need to use:


  ASSIGN ls_rec-value->* TO <fs>.

That's why you are obtaining this type 'l' which is refering to a TYPE REF TO DATA itself. Doing it the right way your final type would be 'C' which corresponds with a character data type.

Best Regards

Read only

0 Kudos
1,867

Ahhh -- yes, thanks

Read only

Former Member
0 Kudos
1,867

Hi

Actually by using field symbols u can refer to the data u want.

Follow this link:

[http://help.sap.com/saphelp_nw70/helpdata/en/fc/eb387a358411d1829f0000e829fbfe/content.htm]

Regards,

Karthik.R