Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a public type of an object

Tags:

abap

I have a class named ZCL_RM_SPREADSHEETML.

It has in the Types tab a type called TY_STYLE with visibility 'Public' and it is defined with Direct Type Entry.

When I try to declare in the caller code the following :

DATA : wa_blue_style TYPE zcl_rm_spreadsheetml-ty_style.

I get the following:

The type "ZCL_RM_SPREADSHEETML" has no structure and therefore no
component called "TY_STYLE". .

This makes some sense I guess as ZCL_RM_SPREADSHEETML is a class, also double-clicking TY_STYLE does absolutely nothing.

Then I tried the following with a tilda:

DATA : wa_blue_style TYPE zcl_rm_spreadsheetml~ty_style.

I got the following:

Type "ZCL_RM_SPREADSHEETML~TY_STYLE" is unknown

Double clicking TY_STYLE will bring me though to the definition of TY_STYLE, so I must be close. The last time I had a similar issue it was because I was accessing a private method, but I marked the type clearly as Public.

Any ideas of what I am doing wrong?

EDIT

I also tried per the comment

DATA : wa_blue_style TYPE ref to zcl_rm_spreadsheetml->ty_style. "and
DATA : wa_blue_style TYPE zcl_rm_spreadsheetml->ty_style. 

which gives

Field "ZCL_RM_SPREADSHEETML" is unknown. It is neither in one of the
specified tables nor defined by a "DATA" statement.

Which gave me the idea to try this the 'class' way,

DATA : wa_blue_style TYPE zcl_rm_spreadsheetml=>ty_style.

This works

like image 602
tomdemuyt Avatar asked Mar 08 '13 21:03

tomdemuyt


People also ask

How do you find a type object?

Get and print the type of an object: type() type() returns the type of an object. You can use this to get and print the type of a variable and a literal like typeof in other programming languages. The return value of type() is type object such as str or int .

Can objects contain public data?

You're talking about the same object reference, and yes it is possible. All objects are effectively public as all Object methods are public. If you can get a reference to it via any means, you have access to that object.

How do you access objects in the classroom?

How to access the object in the class? Explanation: Objects in the method can be accessed using direct member access operator which is (.).

How do you check if an object is of a certain type Java?

The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.


2 Answers

You have to use the appropriate component selector:

Defined character that can be used to address components of upper units. There is a structure component selector (-), a class component selector (=>), an interface component selector (~), and an object component selector (->).

In this case, you're accessing a type (component) of a class, so you have to use =>.

like image 50
vwegert Avatar answered Sep 19 '22 18:09

vwegert


You meant this, right?

report  zstructsob.

*&---------------------------------------------------------------------*
*&       Class MYCLASS
*&---------------------------------------------------------------------*
*        Text
*----------------------------------------------------------------------*
class myclass definition.
  public section.

    types: begin of mystruct, " ------------> The public type
      field1 type i,
      field2 type string,
    end of mystruct.

    methods print_data importing data type mystruct.

  private section.
    data mydata type mystruct.
endclass.               "MYCLASS

*&---------------------------------------------------------------------*
*&       Class (Implementation)  MYCLASS
*&---------------------------------------------------------------------*
*        Text
*----------------------------------------------------------------------*
class myclass implementation.
  method print_data.
    write:/ data-field1, data-field2.
  endmethod.

endclass.               "MYCLASS

start-of-selection.

data ztype type myclass=>mystruct. " ------------> The public type of the class
data zclass type ref to myclass.

create object zclass.

ztype-field1 = 1.
ztype-field2 = 'Field2'.

zclass->print_data( ztype ).
like image 29
Nelson Miranda Avatar answered Sep 17 '22 18:09

Nelson Miranda