on 11-15-2005 3:34 PM
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
HI shahul,
In the method,try the following code
concatenate table_name
'[]'
into v_tablename.
result[] = (v_tablename).
Regards,
Ravi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Shahul,
What is the aim of doing like this???
Thanks
Kam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Not sure if you can do this with a class/method, but you can using fields symbols.
Regards,
Rich Heilman
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
User | Count |
---|---|
77 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.