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: 

Determine if Concrete Type of Abstract Field Symbol is a Numeric type

alex_campbell
Contributor
0 Kudos
197

Hi All,

I hope there isn't a really obvious answer to this, but it's a difficult question to Google because there are 1,000 posts out there asking how to determine if a field contains only numeric characters. That is NOT my question.

I'm trying to find a good way to determine whether the actual type of a field that I've assigned to a field symbol is a numeric type. Here is an example, where i've used the fake keyword 'IS NUMERIC' to represent the logic i'm looking for:

FIELD-SYMBOLS: <l_field> TYPE data.
DO.
  ASSIGN COMPONENT sy-index OF STRUCTURE la_structure TO <l_field>.
  IF sy-subrc <> 0. EXIT. ENDIF.

  IF <l_field> IS NUMERIC.
    MULTIPLY <l_field> BY -1.
  ENDIF.
ENDDO.

In this example, if the field which i've assigned to <l_field> is not a numeric type, i'll get a short dump. What's the best way to handle this?

Thanks,

Alex

1 ACCEPTED SOLUTION

former_member16322
Participant
0 Kudos
123

Please look at RTTI classes.

CL_ABAP_TYPEDESCR, CL_ABAP_ELEMDESCR, etc.

They have methods that can be used to determine this.

6 REPLIES 6

Former Member
0 Kudos
123

hello,

Try this,

First check if the field symbol is assigned -

IF <l_field> IS ASSIGNED.

** Inside this do Catch-Endcatch 

DATA: lv_packed_number     TYPE p.                        "#EC NEEDED

  CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.

    lv_packed_number = <l_field>.

  ENDCATCH.

  IF sy-subrc EQ 0.

" <l_field> is a number

  ENDIF.

  

ENDIF.

best regards,

swanand

former_member16322
Participant
0 Kudos
124

Please look at RTTI classes.

CL_ABAP_TYPEDESCR, CL_ABAP_ELEMDESCR, etc.

They have methods that can be used to determine this.

0 Kudos
123

I had considered using GET_DATA_TYPE_KIND but I wanted to avoid hardcoding the numeric data types if possible (I know SAP won't add any, but it seems odd that there isn't some built in functionality for this. If nothing else, I guess I can just put them all in a range.

I am on ECC5, is there a chance that you're referring to a more direct method that I don't have available in my system?

0 Kudos
123

I don't remember if there is; I'll have to see if I can find my code where I do similar; but there may be an exception you can catch on the ASSIGN statement which would indicate type mismatch. Similar to cx_sy_move_cast_error.

Catching that exception and handling it would prevent hard coding.

Further, Swanands idea of assigning without caring about type (I'm assuming field symbol typed to any), then catching the no number exception would work well to.

Normally, I need to know the type before I decide what to do, so I use RTTI.

Edit - Per the documentation for assign, http://help.sap.com/abapdocu_702/en/abapassign.htm, we do not have a catchable exception to use.

raymond_giuseppi
Active Contributor
0 Kudos
123

Did you try to use CL_ABAP_CLASSDESC method GET_DATA_TYPE_KIND  or even a DESCRIBE FIELD with option TYPE  ?

Regards,

Raymond

0 Kudos
123

To be honest DESCRIBE FIELD had completely slipped my mind. I have the same reservation here as I laid out in my reply to Steve, but if there isn't a more direct way this will probably be best.