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

Dereferencing a generic reference to a structure

Jocelyn_Dart
Product and Topic Expert
Product and Topic Expert
0 Likes
4,015

Ok I'll admit it - my ABAP is a little rusty but I can usually get by.

However I've tried about 15 options on this one and I can't seem to crack it.

I am trying to derefence a generic reference to a typed structure as follows:

So there is a type

E.g.

TYPES: begin of mytype,

    number type decimal,

    currency type c(5),

  end of mytype.

DATA:

  ls_mydata   TYPE mytype,

  lr_struc       TYPE REF TO DATA.

FIELD-SYMBOLS:

  <fs_mydata> TYPE mydata.

So lr_struc is correctly holding a generic reference to a structure of type mytype.

However if I try to do something like this:

ASSIGN lr_struc->* TO <fs_mydata>.

I get short dumps and move cast errors saying that the source and target types don't match.  Except I can see in the debugger that they do absolutely match in every respect and I can see the data in them without problem.

So far I have tried various combinations of CAST, ASSIGN, ASSIGN... CASTING but no go.

All the examples I have found so far use examples that are only simple variables... not structures.

What I'm trying to do is read the value of number and currency that are held in the reference.

What am I doing wrong... ?

1 ACCEPTED SOLUTION
Read only

ceedee666
SAP Champion
SAP Champion
0 Likes
2,504

Hi Jocelyn,

I tried your code and in my system it worked without any problem.

The only thing I had to do was to change

<fs_mydata> TYPE mydata.

to

<fs_mydata> TYPE mytype.

However, I'm unsure if this was only a typo introduced when creating the blog post. Here is the code I used:

REPORT zcd_test.

TYPES: BEGIN OF mytype,
     number TYPE decfloat16,
     currency TYPE char5,
   END OF mytype.


DATA:
   ls_mydata   TYPE mytype,
   lr_struc    TYPE REF TO data.

FIELD-SYMBOLS:
   <fs_mydata> TYPE mytype.

ASSIGN lr_struc->* TO <fs_mydata>.

BREAK-POINT.


When the break point is reached <fs_mydata> isn't assigned to anything as lr_struc hasn't been created so far. When I change the code to the foillowing:


REPORT zcd_test.

TYPES: BEGIN OF mytype,
     number TYPE decfloat16,
     currency TYPE char5,
   END OF mytype.


DATA:
   ls_mydata   TYPE mytype,
   lr_struc    TYPE REF TO data.

FIELD-SYMBOLS:
   <fs_mydata> TYPE mytype.

CREATE DATA lr_struc TYPE mytype.
ASSIGN lr_struc->* TO <fs_mydata>.

BREAK-POINT.


<fs_mydata> is also assigned when the breakpoint is reached.


Hope this helps.

Christian

Hi Jocelyn,

I tried your code and in my system it worked without any problem.

The only thing I had to do was to change

<fs_mydata> TYPE mydata.

to

<fs_mydata> TYPE mytype.

However, I'm unsure if this was only a typo introduced when creating the blog post. Here is the code I used:

REPORT zcd_test.

TYPES: BEGIN OF mytype,
     number TYPE decfloat16,
     currency TYPE char5,
   END OF mytype.


DATA:
   ls_mydata   TYPE mytype,
   lr_struc    TYPE REF TO data.

FIELD-SYMBOLS:
   <fs_mydata> TYPE mytype.

ASSIGN lr_struc->* TO <fs_mydata>.

BREAK-POINT.


When the break point is reached <fs_mydata> isn't assigned to anything as lr_struc hasn't been created so far. When I change the code to the foillowing:


REPORT zcd_test.

TYPES: BEGIN OF mytype,
     number TYPE decfloat16,
     currency TYPE char5,
   END OF mytype.


DATA:
   ls_mydata   TYPE mytype,
   lr_struc    TYPE REF TO data.

FIELD-SYMBOLS:
   <fs_mydata> TYPE mytype.

CREATE DATA lr_struc TYPE mytype.
ASSIGN lr_struc->* TO <fs_mydata>.

BREAK-POINT.


<fs_mydata> is also assigned when the breakpoint is reached.


Hope this helps.

Christian

4 REPLIES 4
Read only

SharathYaralkattimath
Contributor
0 Likes
2,504

Hi Jocelyn,

you may need to call this statement before assigning it to field symbol?

CREATE DATA lr_struc TYPE mydata.

Thanks,

Sharath

Read only

matt
Active Contributor
0 Likes
2,504

Can you post a complete example? How is lr_struc created?

Read only

ceedee666
SAP Champion
SAP Champion
0 Likes
2,505

Hi Jocelyn,

I tried your code and in my system it worked without any problem.

The only thing I had to do was to change

<fs_mydata> TYPE mydata.

to

<fs_mydata> TYPE mytype.

However, I'm unsure if this was only a typo introduced when creating the blog post. Here is the code I used:

REPORT zcd_test.

TYPES: BEGIN OF mytype,
     number TYPE decfloat16,
     currency TYPE char5,
   END OF mytype.


DATA:
   ls_mydata   TYPE mytype,
   lr_struc    TYPE REF TO data.

FIELD-SYMBOLS:
   <fs_mydata> TYPE mytype.

ASSIGN lr_struc->* TO <fs_mydata>.

BREAK-POINT.


When the break point is reached <fs_mydata> isn't assigned to anything as lr_struc hasn't been created so far. When I change the code to the foillowing:


REPORT zcd_test.

TYPES: BEGIN OF mytype,
     number TYPE decfloat16,
     currency TYPE char5,
   END OF mytype.


DATA:
   ls_mydata   TYPE mytype,
   lr_struc    TYPE REF TO data.

FIELD-SYMBOLS:
   <fs_mydata> TYPE mytype.

CREATE DATA lr_struc TYPE mytype.
ASSIGN lr_struc->* TO <fs_mydata>.

BREAK-POINT.


<fs_mydata> is also assigned when the breakpoint is reached.


Hope this helps.

Christian

Read only

Jocelyn_Dart
Product and Topic Expert
Product and Topic Expert
0 Likes
2,504

Thanks folks... that was very helpful.

Yes you were correct I was one reference away from success.

The answer in the end was something closer to this which is very similar to Christian's answer. 

lr_struc = REF #( ls_source_struc_ref ).
ASSIGN lr_struc->* TO <fs_struc>.
ls_mydata
= CAST mytype( <fs_struc>  )->*.


It's been a rough ride getting used to the new dereferencing approaches - so really appreciate being thrown a couple of lifelines to get me thinking harder.


Thanks all!