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

Declaring a variable based on the value of other variable

Former Member
0 Likes
2,488

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

5 REPLIES 5
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,793

define declare.

data:var1(&1) type c.

end-of-definition.

declare 10.

var1 = '1234567890'.

write var1.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,793

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

Read only

vinod_vemuru2
Active Contributor
0 Likes
1,793

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.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,793

>

> 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

Read only

0 Likes
1,793

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