简体   繁体   中英

Which should be used in ABAP : TYPE or LIKE?

So these both seem to work for me:

TABLES:
  T001,        "Table of Company Codes.
  Z_KNA1_VBRK. "View I created..

DATA:
  CCNAME TYPE T001-BUTXT,   
  CCCURR TYPE T001-WAERS,   
  KNAVBK TYPE Z_KNA1_VBRK,
  AMNICC TYPE Z_KNA1_VBRK-NETWR.

and

DATA:
  CCNAME LIKE T001-BUTXT,   
  CCCURR LIKE T001-WAERS,   
  KNAVBK LIKE Z_KNA1_VBRK,
  AMNICC LIKE Z_KNA1_VBRK-NETWR.

PARAMETERS:
  COMPCODE LIKE T001-BUKRS.

Is there any difference between them technically? Which is preferred / best practice and why?

To get the difference try to compile the following program.

REPORT zzz.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    DATA: ls_t000t TYPE t000,
          ls_t000l LIKE t000.
  ENDMETHOD.
ENDCLASS.

The error message you will get is

Within classes and interfaces, you can only use "TYPE" to refer to ABAP Dictionary types, not "LIKE" or "STRUCTURE".

This is because in the OO context you need to write explicitly TYPE when you actually refer to a type. This is the current state of the art.

Now change your program slightly and try to declare global variables with LIKE and TYPE .

REPORT zzz.

DATA: gs_t000t TYPE t000,
      gs_t000l LIKE t000.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    DATA: ls_t000t TYPE t000.
*         ls_t000l LIKE t000.
  ENDMETHOD.
ENDCLASS.

As you can see there are no compilation errors in this case. In this context TYPE and LIKE are interchangeable, they mean the same. This applies also to the "old" parts of ABAP means of modularization like subroutines and function modules.

However I use the following rule of thumb.

Whenever I refer to a DDIC or local type I use TYPE . If I want to create a variable that is exactly of the same type like other variable I use LIKE . Should the type of the original variable change in the future, the change has to be made only in one place then.

Example.

METHOD main.
   DATA: ls_t000t TYPE t000. "should the type change from T000 to T002
                             "in the future, one has to change it only in one place.
   DATA: ls_t000l LIKE ls_t000t.
ENDMETHOD.

Yes, there is a difference.
You cannot get this difference because you declared your structures via TABLES statement, which is obsolete now and shouldn't be used. TABLES statement declares interface work area with a name identical to Data Dictionary structure.
Therefore both your LIKE and TYPE declarations treat Z_KNA1_VBRK either like data object or DDIC structure correspondingly. In any other case such declaration won't compile because LIKE and TYPE statements are not interchangable .
You should declare separate structures in your program rather than using such obsolete elements. The only allowed exception is data exchange with classic dynpros. To further details, switch to ABAP documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM