‎2007 Jun 28 4:13 AM
I want to call few FM as methods. ANyone has ANy sample example for it?
‎2007 Jun 28 6:30 AM
Hi Prasad,
Syntactical constraints will not allow you to call function modules as methods. What you can do is define some methods in a class and inside the method implementation just call the FM in the same way as you call it from some other place. Then call the method using the syntax used for calling methods.
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: set_make
IMPORTING im_make TYPE string
im_model TYPE string.
ENDCLASS. "zl_lcl_vehicle DEFINITION
CLASS zl_lcl_vehicle IMPLEMENTATION.
METHOD set_make.
call function <FM name>.
ENDMETHOD. "set_make
ENDCLASS. "zl_lcl_vehicle
START-OF-SELECTION.
Create instance of the vehicle class
CREATE OBJECT z_vehicle1.
Normal Syntax for method calls
CALL METHOD: z_vehicle1->set_make EXPORTING im_make = 'HYUNDAI'
im_model = 'SANTRO'.
<b>Award points if found useful.</b>
Regards
Indrajit
‎2007 Jun 28 6:30 AM
Hi Prasad,
Syntactical constraints will not allow you to call function modules as methods. What you can do is define some methods in a class and inside the method implementation just call the FM in the same way as you call it from some other place. Then call the method using the syntax used for calling methods.
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: set_make
IMPORTING im_make TYPE string
im_model TYPE string.
ENDCLASS. "zl_lcl_vehicle DEFINITION
CLASS zl_lcl_vehicle IMPLEMENTATION.
METHOD set_make.
call function <FM name>.
ENDMETHOD. "set_make
ENDCLASS. "zl_lcl_vehicle
START-OF-SELECTION.
Create instance of the vehicle class
CREATE OBJECT z_vehicle1.
Normal Syntax for method calls
CALL METHOD: z_vehicle1->set_make EXPORTING im_make = 'HYUNDAI'
im_model = 'SANTRO'.
<b>Award points if found useful.</b>
Regards
Indrajit
‎2007 Jun 28 6:53 AM
Thx for the reply.
Just few clarifications...
So, the importing, exporting and changing parameters of the FM must match the Methods.
What abt 'table' option option?
DO we handle handle exceptions as events or just let the called FM raise its own messages?
‎2007 Jun 28 7:02 AM
Hi Prasad,
What you can do is -
Import a parameter in method - Export it to FM if required
Import a parameter from FM - Use it in the method if required.
The table option is for importing/exporting tables.
The function module will handle the exceptions defined in its interface and return the value in sy-subrc. You can check it and code some functionality depending on the value of sy-subrc. This depends on your requirement. If the FM does not handle any exception that you want to handle, you can handle it explicitly using exception classes and TRY-CATCH statements.
Let me know if you need any further clarifications.
Award points if found useful.
Regards
Indrajit
‎2007 Jun 28 7:06 AM
It is helpful.
Can u elaborate on the tables option?
I know how to use it in FM's but in classes how do u handle it, especially for passing it to the FM inside the method? Just declare it in changing type of the class?
‎2007 Jun 28 7:36 AM
Hi Prasad,
See the following piece of code.
TYPES: ty_itab TYPE standard table OF sflight.
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of Vehicle class
----
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: set_make
IMPORTING value(im_itab) TYPE ty_itab.
PRIVATE SECTION.
DATA: gt_itab TYPE STANDARD TABLE OF sflight.
ENDCLASS. "zl_lcl_vehicle DEFINITION
&----
*& Class (Implementation) zl_lcl_vehicle
&----
Implementation of Vehicle class
----
CLASS zl_lcl_vehicle IMPLEMENTATION.
METHOD set_make.
CALL FUNCTION 'Z_MOBILE_NUMBER_DIST'
TABLES
t_in_slifght = im_itab.
ENDMETHOD. "set_make
ENDCLASS. "zl_lcl_vehicle
Declaring a reference variable with reference to the vehicle class.
This vareiable contains a reference to an object of theclass
DATA z_vehicle1 TYPE REF TO zl_lcl_vehicle.
DATA : gt_itab TYPE STANDARD TABLE OF sflight.
START-OF-SELECTION.
Create instance of the vehicle class
CREATE OBJECT z_vehicle1.
Normal Syntax for method calls
CALL METHOD: z_vehicle1->set_make EXPORTING im_itab = gt_itab.
_________________________________________________________________
<b>Award points if found useful.</b>
Regards
Indrajit
‎2007 Jun 28 12:45 PM
> It is helpful.
>
> Can u elaborate on the tables option?
>
> I know how to use it in FM's but in classes how do u
> handle it, especially for passing it to the FM inside
> the method? Just declare it in changing type of the
> class?
short answer: yes
TABLES parameters are forbidden in OO context. You should not use them. They work only with STANDARD TABLE WITH HEADER LINE, which are forbidden in ABAP OO. Even when working with FMs you should not use them.
‎2007 Jun 28 8:18 AM
Just tried it.
Seems FM may need to be called 'In Background Task' !!
And I cant seem to make the FM accpept importing parameters from the method.
‎2007 Jun 28 8:21 AM
Hi Prasad,
In the FM define a TABLES parameter and while calling the FM assign the table imported by the method to it.
Award points if useful.
Regards
Indrajit
‎2007 Jun 29 4:51 AM
We can always declare tables without header line. Thats not a problem I think.
I tried to do this and the FM inside the method was not accepting the input parameters from the method.
If Anyone has a sample code for this type of a thing, it would be of immense help.