Skip to Content
author's profile photo Former Member
Former Member

constants

How to declare constants in the program

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

7 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 06:24 AM

    constants: c_value(1) value '1'.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 06:26 AM

    hi,

    constants : c_var type (give any datatype) value '(give a constant value)'.

    Regards,

    Shrita.

    Message was edited by: shrita sharma

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 06:49 AM

    Hi,

    Go thru this Docu.

    CONSTANTS

    Syntax

    CONSTANTS const [options].

    Effect

    This statement declares a constant data object - that is, a constant const. The content of a constant cannot be changed at runtime of an ABAP program. You can only use it as an operands in read positions of ABAP statements. Constants that you declare in the declaration section of a class or an interface belong to static attributes of that class/interface.

    The name conventions apply to the name const. The syntax of the additions options of the CONSTANTS statement, for declaring constants, corresponds to that of the DATA statement for declaring variables. The only differences are that the READ-ONLY addition is not possible and within the declaration of a structure, the INCLUDE statement cannot be used.

    In contrast to the DATA statement, you have to specify an Startwert with the addition VALUE using the CONSTANTS statement. The same restrictions as for the DATA statement apply. This has the following implications for the declaration of constants:

    You cannot declare any constants with a deep data type prior to release 6.10. As of release 6.10 and later, you can also declare deep constants, but you can only specify a start value val for ABAP type string. To assign an initial values to the other deep constants, you have to use IS INITIAL.

    Constant internal tables are always empty, and prior to release 6.40, you can only declare them by referring to an existing table type and you cannot declare any new bound table types (see under DATA - TABLE OF), because you cannot use the addition VALUE in this case. As of release 6.40, the VALUE addition can also be specified when defining internal tables. Therefore, you can create new bound table types using the CONSTANTS statement.

    Note

    If you use the class component selector, you can also use the interface name to access static attributes of interfaces that were declared using CONSTANTS.

    Example

    The statements below declare a numeric constant, a constant structure and a constant reference. You can use the reference in comparisons, for example, or pass it on to procedures.

    CONSTANTS pi(8) TYPE p DECIMALS 14

    VALUE '3.14159265358979'.

    CONSTANTS: BEGIN OF sap_ag,

    zip_code(5) TYPE n VALUE '69189',

    city TYPE string VALUE `Walldorf`,

    country TYPE string VALUE `Germany`,

    END OF sap_ag.

    CONSTANTS null_pointer TYPE REF TO object VALUE IS INITIAL.

    Thanks

    Sunil

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 06:54 AM

    Can i declare constants for list heading like name date time if so how

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 06:55 AM

    Elementary constants:

    CONSTANTS: pi TYPE p DECIMALS 10 VALUE '3.1415926536'.

    ref_c1 TYPE REF TO C1 VALUE IS INITIAL.

    The last line shows how you can use the IS INITIAL argument in the VALUEaddition. Since you must use the VALUE addition in the CONSTANTS statement, this is the only way to assign an initial value to a constant when you declare it.

    Complex constants:

    CONSTANTS: BEGIN OF myaddress,

    name TYPE c LENGTH 20 VALUE 'Fred Flintstone',

    street TYPE c LENGTH 20 VALUE 'Cave Avenue',

    number TYPE p VALUE 11,

    postcode TYPE n LENGTH 5 VALUE 98765,

    city TYPE c LENGTH 20 VALUE 'Bedrock',

    END OF myaddress.

    This declares a constant structure myaddress. The components can be addressed by myaddress-name, myaddress-street, and so on, but they cannot be changed.

    Constants are named data objects that you create statically using a declarative statement. They allow you to store data under a particular name within the memory area of a program. The value of a constant must be defined when you declare it. It cannot subsequently be changed. The value of a constant cannot be changed during the execution of the program. If you try to change the value of a constant, a syntax error or runtime error occurs.

    You declare constants using the CONSTANTS statement. Within the program, you can also declare local variables within procedures using CONSTANTS. The same rules of visibility apply to constants as apply to the visibility of data types. Local constants in procedures obscure identically-named variables in the main program. Constants exist for as long as the context in which they are declared.

    The syntax of the CONSTANTS statement is exactly the same as that of the DATA statement, but with the following exceptions:

    · You must use the VALUE addition in the CONSTANTS statement. The start value specified in the VALUE addition cannot be changed during the execution of the program.

    · You cannot define constants for XSTRINGS, references, internal tables, or structures containing internal tables.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 07:06 AM

    Hi,

    It is always a good practise to write keywork in ABAP editor and press F1.

    This will give SAP Documenatation with example.

    This will help you in future in any case.

    So in your current case ...

    type CONSTANTS and press F1 on that.

    Check whether it satisfies your doubts.

    *Please mark all informative responses.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 18, 2006 at 07:47 AM

    Hello,

    check this out.

    constants : c_var <b>type</b> (give any datatype) <b>value</b> '(give a constant value)'.

    example,

    - date type sy-datum value '20061018'.

    - int type i value 10.

    Regards,

    Shehryar

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.