Application Development 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: 

Creation of Multiple Variables of same type at runtime

Former Member
0 Kudos

Hi,

I have a requirement in which I need to create multiple variables at run time . The variables should be TRPE REF TO CL_GENIOS_VARIABLE. The number of variables required will be determined at run time based on the number of materials in a Bill of material. We are using these variables for some calculations as GENIOS is a SAP given code to solve linear equations.

Please help me on this. If some one can let me know how I can create field symbols dynamically with different name that will also help.

5 REPLIES 5

tolga_polat
Active Participant
0 Kudos

Hi Braham,

I didnt use cl_genios_variable, but for your case i think you can do like this:

TYPES : Begin of lst_genios,

                   genios TYPE REF TO cl_genios_variable,

                End of lst_genios.

DATA : ls_genios TYPE lst_genios,

              lt_genios TYPE STANDARD TABLE OF lst_genios.

DATA : ls_details type GENIOSS_VARIABLE_DETAILS,

              lv_count type i

lv_count = 5. " How many variable you needed

do lv_count times.

" fill ls_details with your data

ls_details-name = 'DUMMY'. " you can concatenate name like 'DUMMY' index into name.

ls_details-TYPE = " I dunno what type can be use 😄

" etc.

" create your object

create object ls_genios-genios

     exporting

          IS_DETAILS = ls_details.

Append ls_genios

         To lt_genios.

clear : ls_genios.

enddo.

" Now you have your table with 5 variable. to read data

READ TABLE lt_genios

               INTO ls_genios

      WITH KEY  IF_GENIOS_OBJECT~GV_INDEX = 1

                           IF_GENIOS_OBJECT~GV_NAME = 'DUMMY'.

" Or  INDEX 1. etc

Hope this help.

Tolga POLAT

0 Kudos

Hi Tolga,

Didnt understand the part

  1. create object ls_genios-genios 
  2.      exporting 
  3.           IS_DETAILS = ls_details. " you have name or index in this structure 

0 Kudos

This solution will not work as we cannot create the object of CL_GENIOS_VARIABLE out side the class

0 Kudos

Hi,

The same can be achieved without having to declare a separate types also as following:

   REPORT  zkk_objects.

*----------------------------------------------------------------------*
*       CLASS lcl_test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_test DEFINITION.

ENDCLASS.                    "lcl_test DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_test IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_test IMPLEMENTATION.

ENDCLASS.                    "lcl_test IMPLEMENTATION

DATA:
  gt_objects      TYPE STANDARD TABLE OF REF TO lcl_test,

  dyn_data        TYPE REF TO data      .

FIELD-SYMBOLS:
  <fs_data>       type REF TO lcl_test.



START-OF-SELECTION.

  CREATE DATA dyn_data LIKE LINE OF gt_objects.
  assign dyn_data->* to <fs_data>.

  CREATE OBJECT <fs_data>.

  DO 10 TIMES.

    APPEND <fs_data> TO gt_objects.

  ENDDO.

Here gt_objects will hold all your objects.

Regards,

Kartik

0 Kudos

*----------------------------------------------------------------------*

*       CLASS lcl_genios DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS lcl_genios DEFINITION.

   PUBLIC SECTION.

     METHODS : constructor.

     METHODS : create_model

                IMPORTING

                  i_name TYPE genios_name,

               create_variable

                  IMPORTING

                    i_type TYPE genios_variabletype DEFAULT if_genios_model_c=>gc_var_continuous

                    i_lowerbound TYPE genios_float DEFAULT 0

                    i_upperbound TYPE genios_float OPTIONAL

                    i_name TYPE genios_name.

   PRIVATE SECTION.

     DATA : lo_enviroment TYPE REF TO cl_genios_environment,

            lo_model TYPE REF TO cl_genios_model,

            lo_variable TYPE REF TO cl_genios_variable.

ENDCLASS.                    "lcl_genios DEFINITION

*----------------------------------------------------------------------*

*       CLASS lcl_genios IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS lcl_genios IMPLEMENTATION.

   METHOD constructor.

     lo_enviroment ?= cl_genios_environment=>get_environment( ).

   ENDMETHOD.                    "constructor

   METHOD create_model.

     lo_model = lo_enviroment->create_model( 'DUMMY' ).

   ENDMETHOD.                    "create_model

   METHOD create_variable.

     lo_variable = lo_model->create_variable( iv_type       = i_type

                                              iv_lowerbound = i_lowerbound

                                              iv_upperbound = i_upperbound

                                              iv_name       = i_name       ).

   ENDMETHOD.                    "create_variable

ENDCLASS.                    "lcl_genios IMPLEMENTATION

DATA : lo_genios TYPE REF TO lcl_genios.

START-OF-SELECTION.

   CREATE OBJECT lo_genios.

   lo_genios->create_model( 'DUMMY' ).

   lo_genios->create_variable( i_lowerbound = 1

                               i_upperbound = 255

                               i_name = 'DUMMY' ).

You can create genios_variable like this.

and change this :

  1. TYPES : Begin of lst_genios, 
  2.                    genios TYPE REF TO cl_genios_variable, 
  3.                 End of lst_genios. 

like this :

  1. TYPES : Begin of lst_genios, 
  2.                    genios TYPE REF TO lcl_genios, 
  3.                 End of lst_genios. 


or you can create your table as Kartik example.