Application Development and Automation 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: 
Read only

Like and Type

Former Member
0 Likes
660

Hi,

If i declare the variable as:

ekgrp like eban-ekgrp.

ekgrp type eban-ekgrp.

Please tell the difference between 'like' and 'type'.

Thank you.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
589

Hi,

With like we have to use field names. where as for type we can use dataelements.

Thanks,

Suma

4 REPLIES 4
Read only

Former Member
0 Likes
590

Hi,

With like we have to use field names. where as for type we can use dataelements.

Thanks,

Suma

Read only

Former Member
0 Likes
589

TYPE is used while refering to the data types and types declared using types statement, where as LIKE is used to refer to the data objects.

LIKE means the datatype of the variable is similar to the referenced variable.

TYPE means it is a predefined data type.

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb2ff3358411d1829f0000e829fbfe/frameset.htm

For TYPE

http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

For LIKE

http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb367a358411d1829f0000e829fbfe/content.htm

Eg:

DATA int TYPE i.

Here int is of integer data type.

DATA var LIKE int.

var IS a variable having same data type of int. which in turn is integer.

You can find these helpful when you reference database table variables... You need not know what is the datatype defined.

Also it adds to FLEXIBILITY.

Whenever you make changes to your database tables and fields,

that change is REFLECTED back to your program that is,

You need not change all your program code when you change your table fields.

<REMOVED BY MODERATOR>

Edited by: Alvaro Tejada Galindo on Feb 5, 2008 11:26 AM

Read only

Former Member
0 Likes
589

if you give it by LIKE then sap assign data element by going to the table and then to the field element's data element

but if you assign it by type then SAP directly assignes it to the data element

for more information

You use the LIKE addition, similarly to the TYP E addition , in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The addition

LIKE <obj>

can be used in the same ABAP statements as the TYPE addition to refer to any data object <obj> that is already visible at that point in the program. The expression <obj> is either the name of the data object or the expression

LINE OF <table-object>

In this case, the LIKE addition describes the line type of a table object that is visible at that point in the program.

You use LIKE to make the new object or type inherit the technical attributes of an existing data object.

ABAP Statements with LIKE References

  • Definition of local types in a program using

TYPES <t> LIKE <obj>.

The new data type <t> inherits all of the technical attributes of the data object <obj>.

  • Declaration of data objects using

DATA <f> LIKE <obj>.

CLASS-DATA <f> LIKE <obj>.

CONSTANTS <f> LIKE <obj>.

STATICS <f> LIKE <obj>.

PARAMETERS <f> LIKE <obj>.

The data object <f> inherits all of the technical attributes of the data object <obj>.

  • Dynamic creation of data objects using

CREATE DATA <dref> LIKE <obj>.

  • Specification of the type of a formal parameter in a subroutine using

FORM <sub> ... USING|CHANGING <p> LIKE <obj> ...

The technical attributes of the formal parameter <p> are inherited from those of the declared data object <obj>. You can then only pass actual parameters that have these attributes.

  • Specification of the type of a formal parameter in a method using

METHODS <meth> ... IMPORTING|EXPORTING|CHANGING <p> LIKE <obj> ...

The technical attributes of the formal parameter <p> are inherited from those of the declared data type <type>. You can then only pass actual parameters that have these attributes.

  • Specification of the type of a field symbol

FIELD-SYMBOLS <fs> LIKE <obj>.

The technical attributes of the field symbol <FS> are inherited from those of the declared data object <obj>. You can then only assign data objects that have these attributes.

Visibility of Data Objects

As a rule, you can use LIKE to refer to any object that has been declared using DATA or a similar statement, and is visible in the current context. The object only has to have been declared. It is irrelevant whether the data object already exists in memory when you make the LIKE reference.

  • In principle, the local data objects in the same program are visible. As with local data types, there is a difference between local data objects in procedures and global data objects. Data objects defined in a procedure obscure other objects with the same name that are declared in the global declarations of the program.

  • You can also refer to the data objects of other visible ABAP programs. These might be, for example, the visible attributes of global classes in class pools. If a global class <cl_global> has a public instance attribute or static attribute <attr>, you can refer to it as follows in any ABAP program:

DATA <ref> TYPE REF TO <cl_global>.

DATA: f1 LIKE <cl_global>=><attr>,

f2 LIKE <ref>-><attr>.

You can access the technical properties of an instance attribute using the class name and a reference variable without first having to create an object. The properties of the attributes of a class are not instance-specific and belong to the static attributes of the class.

  • To ensure compatibility with previous releases, you can use the LIKE addition to refer to the data types of database tables and flat structures in the ABAP Dictionary. The LIKE addition searches first for a data object <obj> in the program, then in the ABAP Dictionary for a database table or flat structure with the same name. You can no longer use this kind of type reference in ABAP Objects classes. You should also avoid using the LIKE addition in other ABAP programs except to refer to data objects. To refer to data types, you should use the TYPE addition instead.

u use the TYPE addition in various ABAP statements for defining data types and specifying the types of interface parameters or field symbols. The TYPE addition can have various meanings depending on the syntax and context.

Referring to Known Data Types

You can use the addition

TYPE <type>

to refer to any data type <type> that is already known at this point in the program. It can be used in any of the statements listed below. The expression <obj> is either the name of the data object or the expression

LINE OF <table-type>

In this case, the TYPE addition describes the line type of a table type <table-type> that is visible at that point in the program.

ABAP Statements with TYPE References

  • Definition of local program types using

TYPES <t> TYPE <type>.

The new data type <t> has the same type as <type>.

  • Declaration of data objects using

DATA <f> TYPE <type>.

CLASS-DATA <f> TYPE <type>.

CONSTANTS <f> TYPE <type>.

STATICS <f> TYPE <type>.

PARAMETERS <f> TYPE <type>.

The data object <f> has a data type corresponding to the type <type>.

  • Dynamic creation of data objects using

CREATE DATA <dref> TYPE <type>.

  • Specification of the type of a formal parameter in a subroutine using

FORM <sub> ... USING|CHANGING <p> TYPE <type> ...

The technical attributes of the formal parameter <p> are inherited from those of the declared data type <type>. You can then only pass actual parameters that have these attributes.

  • Specification of the type of a formal parameter in a method using

METHODS <meth> ... IMPORTING|EXPORTING|CHANGING <p> TYPE <type> ...

The technical attributes of the formal parameter <p> are inherited from those of the declared data type <type>. You can then only pass actual parameters that have these attributes.

  • Specification of the type of a field symbol

FIELD-SYMBOLS <fs> TYPE <type>.

The technical attributes of the field symbol <FS> are inherited from those of the declared data type <type>. You can then only pass actual parameters that have these attributes.

Visibility of Data Types

When you refer to known data types using the TYPE addition, the visibility of the data types is important.

  • The predefined ABAP types (C, D, F, I, N, P, T, and X) are always visible. You cannot declare types with the same names as these data types, either in the program or in the ABAP Dictionary.

  • When we talk about the visibility of local data types in the program, we must differentiate between local data types in procedures and global data types. Data types defined in a procedure obscure other objects with the same name that are declared in the global declarations of the program. All local data types in a program obscure data types with the same names in the ABAP Dictionary. This also applies to data types from type groups.

  • In the ABAP Dictionary, different visibility rules apply to standalone data types and the data types stored in type groups. Data types in type groups obscure standalone data types with the same names. However, this should be an exceptional situation. All data types in the ABAP Dictionary should be in the same namespace. When you create a standalone data type, the system displays a warning if the name begins with the name of a type group followed by an underscore. Equally, you cannot create a type group if there is already a standalone data type with the same name followed by an underscore.

The graphic shows the visibility of local and ABAP Dictionary data types:

This graphic is explained in the accompanying text

The system searches from the inside out. If you specify TYPE1 in a TYPE addition in the program , the system uses the ABAP Dictionary type both in the procedure and the main program. If you specify TYPE2 in the procedure, the system uses the local type from the procedure. However, if you specify TYP2 in the main program, the system uses the type from the main program. TYPE2 from the ABAP Dictionary is obscured. If you specify TYPE3 either in the procedure or the main program, the system uses the type from the main program TYPE3 from the ABAP Dictionary is obscured.

Constructing New Data Types

The TYPE addition allows you to construct new data types in the TYPES, DATA; CONSTANTS; and STATICS statements. In the TYPES statement, these are local data types in the program. In the other statements, they are attributes of new data objects.

You can use the following type constructors with the TYPE addition:

  • For references

REF TO <class>|<interface>

  • For structures

BEGIN OF <struct>.

...

END OF <struct>.

  • For tables

<tabkind> OF <linetype> [WITH <key>]

These data types only exist during the runtime of the ABAP program.

<REMOVED BY MODERATOR>

kushagra

Edited by: Alvaro Tejada Galindo on Feb 5, 2008 11:28 AM

Read only

Former Member
0 Likes
589

Thank you.