cancel
Showing results for 
Search instead for 
Did you mean: 

How to get a variable by its name!

Former Member
0 Kudos

Hi Folks,

I have a class with say some 10 internal tables of different types. I want to have a method, which takes the table name as parameter and returns the corresponding table. How could i achieve this?

Let me give a example with the psudo code.

class A

data : table1 type table of i.

data : table2 type table of x.

...

data: table10 type table of c.

the method should be..

getTable(table_name type string) returns result type any table.

{

}

I dont want to use any if or case statement. for example,

-


if table_name = 'table1'

result = table1.

elseif table_name = 'table2'

result = table2

.....

end if.

-


Please suggest some other way where i can get the variable using its literal name.

Thanks in advance!

Regards

Shahul

Accepted Solutions (1)

Accepted Solutions (1)

former_member181962
Active Contributor
0 Kudos

HI shahul,

In the method,try the following code

concatenate table_name

'[]'

into v_tablename.

result[] = (v_tablename).

Regards,

Ravi

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Sucess!!!! Check this sample program out.



report zrich_0001 .

<b>field-symbols: <fs> type table.</b>

*---------------------------------------------------------------------*
*       CLASS lclapp DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lclapp definition.

  public section.

    data: itaba type table of i.
    data: xtaba type i.

    data: itabb type table of i.
    data: xtabb type i.


    methods: constructor,
             get_table importing im_table type string
                       exporting ex_table type any table.


endclass.


*---------------------------------------------------------------------*
*       CLASS lclapp IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lclapp implementation.

  method constructor.

    xtaba = 1.  append xtaba to itaba.
    xtaba = 2.  append xtaba to itaba.
    xtaba = 3.  append xtaba to itaba.


    xtabb = 10.  append xtabb to itabb.
    xtabb = 20.  append xtabb to itabb.
    xtabb = 30.  append xtabb to itabb.

  endmethod.

<b>  method get_table.

    data: internaltable(30) type c.
    concatenate im_table '[]' into internaltable.
    assign (internaltable) to <fs>.
    ex_table = <fs>.

  endmethod.</b>

endclass.

data: myapp type ref to lclapp.
data: imytab type table of i.
data: xmytab type i.

start-of-selection.

  create object myapp.


<b>  call method myapp->get_table
       exporting
            im_table = 'ITABB'
       importing
            ex_table = imytab.</b>


  loop at imytab into xmytab.
    write:/ xmytab.
  endloop.


Regards,

Rich Heilman

Former Member
0 Kudos

Hi guys,

I dont want to loop through a particular table. out of 100 tables i want to choose a particular one based on its name.

Ravi,

It gives syntax error. It doesn't identify '(v_tablename)'.

Thanks

Shahul

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Check my last post.

Welcome to SDN!! Please make sure to award points for helpful answers and mark your posts as solved when you problem is solved completely. Thanks.

Regards,

Rich Heilman

Former Member
0 Kudos

Shahul,

Go with Rich method. It looks fine.

Thanks

Kam

Former Member
0 Kudos

Hi Rich,

Thanks a lot!

It works. I tried to reward your answer.. But it gives some error message.

I will defintely reward it..

Thanks and Regards

Shahul

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Yep, looks like there is something going on with the system right now. Please try again later. Thanks.

Glad that it worked out for ya.

Regards,

Rich Heilman

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

Try a similar code...

data: d_text_x(8),

d_aux(8).

  • Code with case or if statement wich will be substituted

*case p_data.

  • when '01'.

  • write text-001.

  • when '02'.

  • write text-002.

  • when '03'.

  • write text-003.

  • when '04'.

  • write text-004.

  • when '05'.

  • write text-005.

  • when '06'.

  • write text-006.

  • when '07'.

  • write text-007.

  • when '08'.

  • write text-008.

  • when '09'.

  • write text-009.

  • when '10'.

  • write text-010.

  • when '11'.

  • write text-011.

  • when '12'.

  • write text-012.

*endcase.

  • Substituted by:

concatenate 'text-0' p_data into d_text_x.

write (d_text_x) to d_aux.

write d_aux.

This is a very easy example. It's similar to field-symbols.

Cheers,

Mireia

Former Member
0 Kudos

Shahul,

What is the aim of doing like this???

Thanks

Kam

Former Member
0 Kudos

Hi Kam,

I may have 100 internal tables as attributes in my class. I want to get a particular itnernal table through a method. These tables serve different purpose... for ex one table holds list of employees.. other holds list of extensions. I want to have a method which will return only the particular internal table with particular name. I can achieve the same using if or case statements. I want to know if its possible to do it directly.

Note : In the select statement, u can spefiy the table name in runtime...(ex:select * from table (tabname)) i want to apply the similar funda.

Thanks

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Not sure if you can do this with a class/method, but you can using fields symbols.

Regards,

Rich Heilman

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Here is what I mean.



report zrich_0001 .

*---------------------------------------------------------------------*
*       CLASS lclapp DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lclapp definition.

  public section.

    data: itaba type table of i.
    data: xtaba type i.

    data: itabb type table of i.
    data: xtabb type i.


    methods: constructor,
             get_table importing im_table type string
                       exporting ex_table type any table.


endclass.


*---------------------------------------------------------------------*
*       CLASS lclapp IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lclapp implementation.

  method constructor.

    xtaba = 1.  append xtaba to itaba.
    xtaba = 2.  append xtaba to itaba.
    xtaba = 3.  append xtaba to itaba.


    xtabb = 10.  append xtabb to itabb.
    xtabb = 20.  append xtabb to itabb.
    xtabb = 30.  append xtabb to itabb.

  endmethod.

  method get_table.


  endmethod.

endclass.

data: myapp type ref to lclapp.
data: xmytab type i.
<b>field-symbols: <fs> type table.</b>

start-of-selection.

  create object myapp.

<b>  assign myapp->itaba to <fs>.</b>

  loop at <fs> into xmytab.
    write:/ xmytab.
  endloop.


Regards,

Rich Heilman