10-01-2008 6:11 AM
Hi Experts,
How to find out the field name of a internal table during the run time.
FIELD-SYMBOLS : <wa> TYPE ANY,
<comp> TYPE ANY.
FIELD-SYMBOLS : <wa1> TYPE ANY,
<comp1> TYPE ANY.
ASSIGN gs_old_document TO <wa>.
ASSIGN gs_new_document TO <wa1>.
DO 15 TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <comp>.
ASSIGN COMPONENT sy-index OF STRUCTURE <wa1> TO <comp1>.
IF <comp> NE <comp1>.
ls_intellilog-slno = 1.
ls_intellilog-changedon = sy-datum.
ls_intellilog-changedby = sy-uname.
ls_intellilog-fldname
ls_intellilog-old_value = <comp>.
ls_intellilog-new_value = <comp1>.
APPEND ls_intellilog TO lt_intellilog.
enddo.
Here i want the field name of the <comp> . can u help to solve the issue.
Thanbks and regards
Renjith MP
10-01-2008 8:27 AM
Hi,
Use the describe by data method instead, e.g.
data: gs_t001 type t001.
data: go_struct type ref to cl_abap_structdescr,
gt_comp type abap_component_tab,
gs_comp type abap_componentdescr.
start-of-selection.
go_struct ?= cl_abap_typedescr=>describe_by_data( gs_t001 ).
gt_comp = go_struct->get_components( ).
loop at gt_comp into gs_comp.
write: / gs_comp-name.
endloop.
Darren
09-22-2015 3:39 PM
The solution works, but IMHO contains a logical bug. You define go_struct as "type ref to cl_abap_structdescr", but then fill it through a different class "cl_abap_typedescr". I am puzzled that this works, but I found that it also works if you do it consistently and code
go_struct ?= cl_abap_structdescr=>describe_by_data( gs_t001 ).
09-22-2015 4:28 PM
Hi Jens,
It's not a Logical bug - it's called inheritance.
Due to the fact, that class-methods are available throughout the hole hierarchy of inheritance, you cann access them through each class in the tree..
cl_abap_structdescr is derived from cl_abap_typedescr but has some added functionality.
In order to address the components of a struct, you have to get Access via a reference to cl_abap_structdescr. The casting Operator '?=' takes care, that the returned object is of the correct type.
Best regards - Jörg
09-23-2015 8:56 AM
Hi Jörg,
I see your point. It explains why it technically works.
Nonetheless I consider it unclean to define a variable referring to one class and then filling it with another. There is no reason not to refer to the right (exact) class in the casting assignment.
Speaking of which, how is it possible to see all sub-classes of a class? In SE24 I can see the superclass of a class in the properties, but fail to find a way to see its (potential) set of sub-classes.
09-23-2015 10:47 AM
Jens Petersen wrote:
(.....)
Nonetheless I consider it unclean to define a variable referring to one class and then filling it with another. There is no reason not to refer to the right (exact) class in the casting assignment.
(....)
THAT is called "casting" and the reason for "?=" instead of "=".
08-04-2010 4:30 PM