Application Development 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: 

Assignment of a structure to field symbol possible?

alexander_kutz
Participant
0 Kudos

Dear all,

I use a method with the following parameters:

Parameters:

DATA_STRUCTURE    TYPE ANY (contains a data structure)

DATA_LINE    TYPE ANY (is one row of a table)

The following does work:

FIELD-SYMBOLS:
         <name> TYPE ANY.

ASSIGN COMPONENT 'myname' OF STRUCTURE DATA_LINE TO <name>.

<name> = 'Clinton'.

Now my question:

Is it possible to cast the complete structure to a field symbol (to change the line) instead of addressing each element explicit:

e.g.:

DATA_LINE-name = 'Clinton'.


or

<fs_DATA_LINE>-name = 'Clinton'.

?

All my efforts to address one field without the ASSIGN COMPONENT did not work. The method itself should be flexible - without any fix structure.

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello,

in order to be fully flexible, you have to work with RTTI.

Assign your structure to a field symbol, use RTTI (CL_ABAP_TYPEDESCR, CL_ABAP_STRUCTDESCR) to get the components, use ASSIGN COMPONENT with a name of these components.

Horst

10 REPLIES 10

Former Member
0 Kudos

Hi Alexander

I'm having a bit af a problem understanding exactly what the problem is.

Reason being: If you normally assign a field symbol of type any and you would like to assign a structure to it, is that not normally just achieved by loop at assigning <fs>. endloop. or read table <tab> assigning <fs>.

Maybe I'm just misunderstanding you, could you please give a bit more details as to what you are trying to achieve.

Regards

Vic

Former Member
0 Kudos

Hello Alexander,

you can't specify fields when you use "Type any".

you can to do this with 2 ways.

1)

FIELD-SYMBOLS:  <f_bseg> TYPE bseg.

ASSIGN '(program)BSEG' TO <f_bseg>.

IF sy-subrc EQ 0.

     <f_structure>-budat = '20120604'.

ENDIF.

2)

TYPES: begin of ty_structure,

               name like sy-uname,

             end of ty_structure.

DATA wa_structure type ty_structure.

FIELD-SYMBOLS <F_stucture> TYPE ANY.

ASSIGN '(program)structure' TO <f_structure>.

IF sy-subrc eq 0.

     MOVE-CORRESPONDING OF <F_structure> TO wa_structure.

     wa_structure-name = 'Clinton'.

     MOVE-CORRESPONDING OF wa_structure TO <f_structure>.

ENDIF.

regards,

Rogerio

0 Kudos

Thanks for the input... what do you mean with "(program)" ?

In your point 1 you referece to the table, there you have the hard defined structure, clear. In the 2nd point you define a structure directly. Also clear.

I just want to use the structure dynamic - it seems besides ASSIGN COMPONENT is no way. - I have to use a hard defined structure / table or define the structure hard in my code - or I use the flexible "ANY" but with the con to assign element by element.

0 Kudos

I used to assign fields in user-exits, when I change or read some field in a standard program. In this case, it's necessary specify field and program that I'm assigning. But, if it don't happen with you, you don't need specify the program.

In general, you understand well. To assign a complete structure, you can't use Assign Component. This command just works for a single field. To assign a complete structure, you assign using:

ASSIGN 'structure' TO <f_structure>.

If you need change some field in this structure, you can't use "type any" and change the field directly. You have to use TYPE "correct type" or use "move-corresponding".

Maybe you could use another way.

FIELD-SYMBOLS: <f_structure> type any,

                            <f_field> type any.

ASSIGN 'structure' TO <f_structure>.

IF sy-subrc eq 0.

ASSIGN COMPONENT 'name' OF STRUCTURE <f_structure> TO <f_field>.

IF sy-subrc EQ 0.

     <f_field> = 'Clinton'.

ENDIF.

ENDIF.

It works too.

Former Member
0 Kudos

Assign data_line TO <fs_data_line>. should work.

<fs_data_line> = data_line. " assigning value to field symbol.

MOVE data_line TO <fs_data_line>.

Check the basic forms of ASSIGN.

http://help.sap.com/saphelp_470/helpdata/en/fc/eb38d5358411d1829f0000e829fbfe/content.htm

Thanks,

Shambu

0 Kudos

Dear Shambu,

thanks for the reply. The assignment works, yes, but it is still not possible to get direct access to one element in the line (without assigning it directly):

"The data object "<FS_LINE>" has no structure and therefore no component.."

0 Kudos

Hi Alexander,

You can only reference using a field of the structure if its typed with that structure.

data : table type standard table of mara.

For ex. Field-symbols : <line> like line of table.

Then you can refer <line>-matnr.

If you declare <line> as TYPE any, then you need to use ASSIGN COMPONENT.

Thanks,

Shambu

0 Kudos

This message was moderated.

hitesh_gabani
Participant
0 Kudos

Hi Alexander,

Use the below code to assign the data structure to field symbol.

DATA : dref TYPE REF TO data.


FIELD-SYMBOLS : <l_dat> TYPE any.

  

CREATE DATA dref LIKE g_dat.

ASSIGN dref->* TO <l_dat>.

<l_dat> = g_dat.

Regards,

Hitesh Gabani

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello,

in order to be fully flexible, you have to work with RTTI.

Assign your structure to a field symbol, use RTTI (CL_ABAP_TYPEDESCR, CL_ABAP_STRUCTDESCR) to get the components, use ASSIGN COMPONENT with a name of these components.

Horst