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: 

How to access attributes of a object

Former Member
0 Kudos

Hi, I have the following code:

CLASS c1 DEFINITION.

   PUBLIC SECTION.

     DATA a1 TYPE c VALUE '1'.

ENDCLASS.

data: lc_ref TYPE REF TO c1.

CREATE OBJECT lc_ref.

lc_ref->a1 = 9.

write lc_ref->a1. " 9

data: oref1 TYPE REF TO object.

CREATE OBJECT oref1 TYPE c1.

WRITE oref1->a1. " not working!!


how to get the last line working to write a 1?


Moderator message: it's very useful to define precisely what is meant by "not working", rather than leave people to guess. For the benefit of anyone else reading this thread:


Cant activate the code:

Field "A1" is unknown. It is neither in one of the specified tables nor

defined by a "DATA" statement. .

Message was edited by: Matthew Billingham - added important information.

11 REPLIES 11

thanga_prakash
Active Contributor
0 Kudos

Hi Michael,

Try below.

CLASS c1 DEFINITION.

   PUBLIC SECTION.

     DATA a1 TYPE c VALUE '1'.

ENDCLASS.

data: lc_ref TYPE REF TO c1.

CREATE OBJECT lc_ref.

lc_ref->a1 = 9.

write lc_ref->a1. " 9

data: oref1 TYPE REF TO c1.

CREATE OBJECT oref1 TYPE c1.

WRITE oref1->a1. " not working!!

Regards,

Thanga

0 Kudos

I want to keep data: oref1 TYPE REF TO object. because it should be dynamic.

0 Kudos

Hello Michael,

Your code will work and both the results will display like 9 1.. what is the issue?

Thanks,

Sujeet

0 Kudos

Cant activate the code:

Field "A1" is unknown. It is neither in one of the specified tables nor

defined by a "DATA" statement. .

0 Kudos

are you adding your class defination?

CLASS c1 DEFINITION.

    PUBLIC SECTION.

      DATA a1 TYPE c VALUE '1'.

ENDCLASS.

0 Kudos

Hi,

You can never do it if you want to keep that object.

You will need something like below.

Regards

Bill


  CLASS c_base DEFINITION. " you need to define base class
   PUBLIC SECTION.
     DATA a1 TYPE c VALUE '1'.
ENDCLASS.


CLASS c1 DEFINITION
   INHERITING FROM c_base. " And inherit it
ENDCLASS.


DATA: lc_ref TYPE REF TO c1.
CREATE OBJECT lc_ref.
lc_ref->a1 = 9.
WRITE lc_ref->a1. " 9


DATA: oref1 TYPE REF TO c_base. " And use base class for dynamic code
CREATE OBJECT oref1 TYPE c1.
WRITE oref1->a1. " It's working now!!

thanga_prakash
Active Contributor
0 Kudos

This message was moderated.

ThomasKruegl
Participant
0 Kudos

You can use:

ASSIGN ('oref1->a1') TO FIELD-SYMBOL(<lv_attribute>).

IF sy-subrc EQ 0.

  WRITE <lv_attribute>.

ENDIF.

But if at this point you know that this attribute exists, you also know the type of the class, and you could also cast the reference, for example:

DATA(lv_attribute) = CAST c1( oref1 )->a1.

WRITE lv_attribute.

regards,

Thomas

hubert_heitzer
Contributor
0 Kudos

Hi Michael,

if you want to access attribute a1 of reference obj1 you have to assume that obj1 refers to an object of type c1 and do a widening cast.



Michael Werner wrote:

Hi, I have the following code:

CLASS c1 DEFINITION.

   PUBLIC SECTION.

     DATA a1 TYPE c VALUE '1'.

ENDCLASS.

data: lc_ref TYPE REF TO c1.

CREATE OBJECT lc_ref.

lc_ref->a1 = 9.

write lc_ref->a1. " 9

data: oref1 TYPE REF TO object.

CREATE OBJECT oref1 TYPE c1.

WRITE oref1->a1. " not working!!


how to get the last line working to write a 1?


DATA: lr_c1 TYPE REF TO c1.

lr_c1 ?= oref1.

WRITE lr_c1->a1.


Regards,

Hubert

renal_dmello
Explorer
0 Kudos

This message was moderated.

renal_dmello
Explorer
0 Kudos

Hi Michael,

Because in the second case the object allocation to class C1 is dynamic, it gives an error while activating.

All dynamic data needs to be handled via dynamic variables.

You can try with below code.

CLASS c1 DEFINITION.

    PUBLIC SECTION.

      DATA a1 TYPE c VALUE '1'.

ENDCLASS.

FIELD-SYMBOLS: <comp>  TYPE any.

DATA: name TYPE string.

START-OF-SELECTION.

data: lc_ref TYPE REF TO c1.

CREATE OBJECT lc_ref.

lc_ref->a1 = 9.

write lc_ref->a1. " 9

data: oref1 TYPE REF TO OBJECT.

CREATE OBJECT oref1 TYPE c1.


*dynamic allocation of field

name = 'OREF1->A1'.

ASSIGN (name) TO <comp>.

WRITE <COMP>.