Define returning type as packed number for method

1.7k Views Asked by At

I am learning ABAP Objects. I'd like to have an object method returning packed number type. I've made that working finally, but I don't know if it is the correct way and I'd need some further explanation which I can't find online.

For integer, it works fine:

METHODS: getamount RETURNING VALUE(r) TYPE i,

For packed number it doesn't:

METHODS: getamount RETURNING VALUE(r) TYPE p,

Error: The type of RETURNING parameter must be fully specified

METHODS: getamount RETURNING VALUE(r) TYPE p LENGTH 10 DECIMALS 3,

Error: The type of RETURNING parameter must be fully specified

(1) Is there way to make it work with p type?

I made it work by using dec5_2:

getamount RETURNING VALUE(r) TYPE dec5_2

(2) Is it the correct alternative? Is there a list of similar types?

Also, I found this solution, but it doesn't work for me:

CLASS lcl_rowinvoice DEFINITION.
  PUBLIC SECTION.
    METHODS:
      getamount RETURNING VALUE(r) TYPE typeprice,  
  PRIVATE SECTION.
    TYPES:
      typeprice TYPE p LENGTH 10 DECIMALS 2,

Unknown type "TYPEPRICE".

(3) Is there way to make this solution to work?

2

There are 2 best solutions below

0
On BEST ANSWER

Returning parameters have to be fully typed, p is a generic type, so you have three options:

  1. Use a predefined data element from the Data Dictionary (SE11 => Data elements)

 METHODS getamount RETURNING value(r) TYPE netwr.
  1. Use a type which is defined in the PUBLIC section of the local class

TYPES: lty_p TYPE p LENGTH 15 DECIMALS 2.
METHODS getamount RETURNING value(r) TYPE lty_p.
  1. Use fully predefined type (decfloat16 or decfloat34)

METHODS getamount RETURNING value(r) TYPE decfloat16.
0
On

It's a very interesting question you raised here.

It is common rule in ABAP that declaring types and objects is effective only from the line they have been declared:

The defined data type can be viewed within the current context from this position.

But! But here we have an interesting collision with ABAP Objects class definition syntax

CLASS class DEFINITION [class_options]. 
  [PUBLIC SECTION. 
    [components]] 
  [PROTECTED SECTION. 
    [components]] 
  [PRIVATE SECTION. 
    [components]] 
ENDCLASS. 

The mutual position of visibility areas during definition are fixed and you cannot change them as you want.

While it doesn't contradict ABAP visibility concept, technically it's impossible to declare type in private section and use it in public. The declaration position of this type will be below the public section and thus effectively invisible to it.

But if you change the order of the declaration/use parts, all will be compiled fine.

CLASS lcl_rowinvoice DEFINITION.

PUBLIC SECTION.
   TYPES: typeprice TYPE p LENGTH 10 DECIMALS 2.

PRIVATE SECTION.
   METHODS: getamount RETURNING VALUE(r) TYPE typeprice.

ENDCLASS.