2009 Nov 16 2:31 PM
Hi,
I need to declare a variable with reference to the value got from another variable.
Ex: var1 type string.
so, the value optained in var1 is '10'. Based on this value, i need to declare a variable of type CHAR with length equal to the value got in the var1.
i.e. var2 TYPE CHAR10. This 10 is the value got in var1.
I need the var2 in only CHAR format but not in any other format.
Is this possible.
Thanks
rohith
Edited by: Rob Burbank on Nov 16, 2009 9:44 AM
Hi,
I need to declare a variable with reference to the value got from another variable.
Ex: var1 type string.
so, the value optained in var1 is '10'. Based on this value, i need to declare a variable of type CHAR with length equal to the value got in the var1.
i.e. var2 TYPE CHAR10. This 10 is the value got in var1.
I need the var2 in only CHAR format but not in any other format.
Is this possible.
Thanks
rohith
Edited by: Rob Burbank on Nov 16, 2009 9:44 AM
2009 Nov 16 2:37 PM
define declare.
data:var1(&1) type c.
end-of-definition.
declare 10.
var1 = '1234567890'.
write var1.
2009 Nov 16 2:56 PM
Hello,
You can make use of the method GET_C of the RTTS class CL_ABAP_ELEMDESCR for this:
PARAMETER: P_LEN TYPE I OBLIGATORY.
DATA:
ELEM_TYPE TYPE REF TO CL_ABAP_ELEMDESCR,
DREF TYPE REF TO DATA.
FIELD-SYMBOLS: <DYN> TYPE ANY.
TRY.
CALL METHOD CL_ABAP_ELEMDESCR=>GET_C
EXPORTING
P_LENGTH = P_LEN
RECEIVING
P_RESULT = ELEM_TYPE.
CATCH CX_PARAMETER_INVALID_RANGE .
ENDTRY.
CREATE DATA DREF TYPE HANDLE ELEM_TYPE.
ASSIGN DREF->* TO <DYN>.
<DYN> = '1234567890'.
WRITE: <DYN>.@Keshu: I did not know you can use macros for this Anyways i am always skeptical about using them !!
BR,
Suhas
2009 Nov 16 3:11 PM
Hi Rohith,
Why can't you use field symbols. Just check below example.
FIELD-SYMBOLS: <fs_var> TYPE ANY.
DATA: l_num TYPE n VALUE '1',
l_char TYPe c VALUE 'X'.
ASSIGN l_num TO <fs_var>.
WRITE <fs_var>.
UNASSIGN <fs_var>.
ASSIGN l_char TO <fs_var>.
WRITE <fs_var>.
Field symbols works not only for length but you can assign data type also dynamically.
thanks,
Vinod.
2009 Nov 16 3:26 PM
>
> Field symbols works not only for length but you can assign data type also dynamically.
Hello Vinod,
You should rather add 'generically typed' field-symbol to your statement.
Also in the method you have suggested the field-symbol is of generic type, it can hold ANY type of data (I, P, N, D etc.)
If you use the RTTS method to assign the data object to your generic field symbol, the field symbol actually references the characteristics of the data object.
In the code i have suggested reduce the value of P_LEN & see the difference.
Hope i am clear.
BR,
Suhas
2009 Nov 17 5:23 AM
Hi Suhas,
thanx for the solution..
Now, the point is like i need to declare an internal table based on this P_RESULT.
As per the below code, i can create a work area but not the internal table
CREATE DATA DREF TYPE HANDLE ELEM_TYPE.
ASSIGN DREF->* TO <DYN>...
I tried to create it but error says that ASSIGN_TYPE_CONFLICT..
Any help plz..
Thanks
rohith