2023 Feb 28 6:59 AM
I am getting error at line number 4.
ERROR: The "LENGTH" declaration is only valid for types C, N, X, or P.
My Question is: By default an Object 'Ch2' is of type C, then why I am getting this error ?
Thank You.
2023 Feb 28 12:31 PM
As ABAP documentation - DATA, TYPE abap_type says:
Syntax
DATA { {var[(len)] TYPE abap_type [DECIMALS dec]}
| {var [TYPE abap_type [LENGTH len] [DECIMALS dec]]} }
[VALUE val|{IS INITIAL}]
[READ-ONLY].
And if you indicate LENGTH, as you can see above, you must also indicate TYPE.
And it's the answer to your question: By default an Object 'Ch2' is of type C, then why I am getting this error ?
2023 Feb 28 9:12 AM
you are missing a TYPE specification, i.e. DATA ch2 TYPE c LENGTH 20.
2023 Feb 28 9:48 AM
I have missed TYPE specification in Line Number 1 and I am not getting any error at Line Number 1,
but I am getting error at Line Number 4.
What is the underlying reason ?
Thank You.
2023 Feb 28 11:25 AM
You are missing the data type here, that is C.
DATA Ch2 type c LENGTH 20 VALUE 'KIRAN KUMAR'.
However, below syntax reads it as type as C, N, X or P. depending upon the value we pass on to it.
That's why it is not throwing any error.
DATA Ch1(20) VALUE 'KIRAN KUMAR'.
2023 Feb 28 1:12 PM
I do not agree with your answer which says:
"However, below syntax reads it as type as C, N, X or P. depending upon the value we pass on to it."
Because in ABAP, the default data type is Character type.
In the variable declaration statement, if the data type of a variable is not defined then the compiler will assign the data type of a variable as Character Type. I have tested the code by setting break point.
In the below statement:
DATA Ch1(20) VALUE 'KIRAN KUMAR'.
During compile time the compiler will assign data type of a variable Ch1 as Character type.
If my explanation or understanding is not correct please correct me with valid reference or docs.
Thank You.
2023 Feb 28 11:30 AM
Hi,
you have to write the type ( C, D , I) etc.
so in your case you have to write the type C length 20
DATA Ch1(20) VALUE 'KIRAN KUMAR'.
WRITE: / 'Ch1 = ', Ch1.
DATA Ch2 Type C LENGTH 20 VALUE 'KIRAN KUMAR' .
WRITE: / 'Ch2 = ', Ch2.
" you can use the Inline Declaration
DATA(ch3) = 'KIRAN KUMAR'.
WRITE: / 'Ch3 = ', Ch3.
2023 Feb 28 12:31 PM
As ABAP documentation - DATA, TYPE abap_type says:
Syntax
DATA { {var[(len)] TYPE abap_type [DECIMALS dec]}
| {var [TYPE abap_type [LENGTH len] [DECIMALS dec]]} }
[VALUE val|{IS INITIAL}]
[READ-ONLY].
And if you indicate LENGTH, as you can see above, you must also indicate TYPE.
And it's the answer to your question: By default an Object 'Ch2' is of type C, then why I am getting this error ?
2023 Feb 28 1:35 PM
My doubt is clear with your specific answer.
Thank You.
2023 Feb 28 12:40 PM
Read carefully the documentation on DATA syntax. The LENGTH option requires to use a TYPE
DATA { {var[(len)] TYPE abap_type [DECIMALS dec]}
| {var [TYPE abap_type [LENGTH len] [DECIMALS dec]]} }
[VALUE val|{IS INITIAL}]
[READ-ONLY].
2023 Feb 28 1:37 PM
My doubt is clear with your explanation.
Thank you.