Allocortex
Allocortex

Reputation: 3

Dynamic Type definition for a Char Field with variable length

For internal usage I need to define a structured type with one of the fields being a char-field with a variable length.

Something like this (I hope this example clarifies my problem):

DATA: lv_type TYPE char7.
lv_type = 'char128'. "The actual length will be determined during execution of the program

TYPES: BEGIN OF ty_satzcounter,
       satza TYPE zedist,
       addit TYPE (lv_type), "<----- Something like this (obviously, it doesn't work like 
                             "       this, but I think it clarifies my question)
       menge TYPE int1,
       END OF ty_satzcounter.

DATA: lt_satzcounter TYPE TABLE OF ty_satzcounter,
      ls_satzcounter TYPE ty_satzcounter.
...
...

Upvotes: 0

Views: 1582

Answers (2)

futu
futu

Reputation: 897

I am absolutely with JozsefSzikszai's solution, but for demonstrating the additional complexity of dynamic structures and tables, here a possible solution as example:

"your input
DATA(lv_charlength) = 128.
"or
DATA(lv_type) = 'CHAR128'.

DATA:
      lro_structdescr TYPE REF TO cl_abap_structdescr,
      lro_tabledescr  TYPE REF TO cl_abap_tabledescr,
      lro_datadescr   TYPE REF TO cl_abap_datadescr,
      lro_typedescr   TYPE REF TO cl_abap_typedescr,
      lt_component    TYPE abap_component_tab,
      ls_component    TYPE LINE OF abap_component_tab,
      lro_wa          TYPE REF TO data.

    FIELD-SYMBOLS: <fs_wa> TYPE any.
    FIELD-SYMBOLS: <fs_tab> TYPE table.


* determine components of structure -> lt_component    
    ls_component-name = 'SATZA'.
    ls_component-type ?= cl_abap_elemdescr=>describe_by_name( 'ZEDIST' ).
    APPEND ls_component TO lt_component.

    ls_component-name = 'ADDIT'.
    ls_component-type ?= cl_abap_elemdescr=>get_c( p_length = lv_charlength ).
    APPEND ls_component TO lt_component.

    "or
*   ls_component-name = 'ADDIT'.
*   ls_component-type ?= cl_abap_elemdescr=>describe_by_name( lv_type ).
*   APPEND ls_component TO lt_component.

    ls_component-name = 'MENGE'.
    ls_component-type ?= cl_abap_elemdescr=>describe_by_name( 'INT1' ).
    APPEND ls_component TO lt_component.

* get structure descriptor -> lro_STRUCTDESCR
    lro_structdescr ?= cl_abap_structdescr=>create( lt_component ).

* create work area of structure lro_STRUCTDESCR -> lro_WA
    CREATE DATA lro_wa TYPE HANDLE lro_structdescr.
*    ASSIGN lro_wa->* TO <fs_wa>. "work area/structure to use

    lro_datadescr ?= lro_structdescr.
    lro_tabledescr ?= cl_abap_tabledescr=>create( lro_datadescr ).

* Create dynmaic internal table
    CREATE DATA et_range_tab TYPE HANDLE lro_tabledescr.
*    ASSIGN et_range_tab->* TO <fs_tab>. "table to use

Upvotes: 1

J&#243;zsef Szikszai
J&#243;zsef Szikszai

Reputation: 5071

This kind of dynamic typing is not possible, but for your purpose, character type with dynamic length, there is the type string:

TYPES: satza TYPE zedist,
       addit TYPE string,
... 

Upvotes: 2

Related Questions