‎2007 Dec 28 11:55 AM
Hi,
i have a class with several methods. Some of these methods should remember if they are called the first time or not. Of course i can add some protected/private attributes to put this infoirmation in, but isnt there another way of doing that?
‎2007 Dec 28 9:47 PM
Hello Rainer
You could use the keyword STATICS to define static local variables within your methods.
However, STATICS cannot be used within instance methods (see ABAP keyword documentation). Anyway the use of static local variables is not recommended.
I would use a very simple approach to implement this requirement:
(1) Define a static table type attribute which will store the names of the already called methods
(2) Create a static method which checks if the method has already been called
The sample report ZUS_SDN_METHODS_WITH_MEMORY show how to implement this requirement:
&----
*& Report ZUS_SDN_METHODS_WITH_MEMORY
*&
&----
*& Description: class memorized method calls
&----
*& Link: https:||forums.sdn.sap.com/threa.jspa?messageID=4646956
*&
*&
&----
REPORT zus_sdn_methods_with_memory.
----
CLASS lcl_memory DEFINITION
----
*
----
CLASS lcl_memory DEFINITION.
TYPE-POOLS: abap.
PUBLIC SECTION.
CLASS-METHODS:
static_method_1.
METHODS:
instance_method_1,
instance_method_2,
display.
PRIVATE SECTION.
TYPES:
ty_t_methods TYPE STANDARD TABLE OF tmdir
WITH DEFAULT KEY.
CLASS-DATA:
mt_methods TYPE ty_t_methods.
CLASS-METHODS:
has_been_called
IMPORTING
value(id_method) TYPE seocpdname
RETURNING
value(rd_result) TYPE abap_bool.
DATA:
mo_table TYPE REF TO cl_salv_table.
ENDCLASS. "lcl_memory DEFINITION
----
CLASS lcl_memory IMPLEMENTATION
----
*
----
CLASS lcl_memory IMPLEMENTATION.
METHOD static_method_1.
IF ( has_been_called( 'STATIC_METHOD_1' ) = abap_false ).
ELSE.
ENDIF.
ENDMETHOD. "static_method_1
METHOD instance_method_1.
IF ( has_been_called( 'INSTANCE_METHOD_1' ) = abap_false ).
ELSE.
ENDIF.
ENDMETHOD. "instance_method_1
METHOD instance_method_2.
IF ( has_been_called( 'INSTANCE_METHOD_2' ) = abap_false ).
ELSE.
ENDIF.
ENDMETHOD. "instance_method_2
METHOD display.
TRY.
CALL METHOD cl_salv_table=>factory
EXPORTING
LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
R_CONTAINER =
CONTAINER_NAME =
IMPORTING
r_salv_table = me->mo_table
CHANGING
t_table = mt_methods.
CATCH cx_salv_msg .
ENDTRY.
CHECK ( me->mo_table IS BOUND ).
CALL METHOD me->mo_table->display( ).
ENDMETHOD. "display
METHOD has_been_called.
define local data
DATA:
ls_method LIKE LINE OF mt_methods.
READ TABLE mt_methods TRANSPORTING NO FIELDS
WITH KEY table_line = id_method.
BINARY SEARCH.
IF ( syst-subrc = 0 ).
rd_result = abap_true.
ELSE.
rd_result = abap_false.
CLEAR: ls_method.
ls_method-classname = 'LCL_MEMORY'.
DESCRIBE TABLE mt_methods.
ls_method-methodindx = syst-tfill + 1.
ls_method-methodname = id_method.
APPEND ls_method TO mt_methods.
ENDIF.
SORT mt_methods BY table_line.
ENDMETHOD. "has_been_called
ENDCLASS. "lcl_memory IMPLEMENTATION
DATA:
go_memory TYPE REF TO lcl_memory,
go_table TYPE REF TO cl_salv_table.
START-OF-SELECTION.
CREATE OBJECT go_memory.
CALL METHOD go_memory->static_method_1( ).
CALL METHOD go_memory->instance_method_2( ).
CALL METHOD go_memory->instance_method_1( ).
CALL METHOD go_memory->display( ).
END-OF-SELECTION.
Regards,
Uwe
‎2007 Dec 28 9:47 PM
Hello Rainer
You could use the keyword STATICS to define static local variables within your methods.
However, STATICS cannot be used within instance methods (see ABAP keyword documentation). Anyway the use of static local variables is not recommended.
I would use a very simple approach to implement this requirement:
(1) Define a static table type attribute which will store the names of the already called methods
(2) Create a static method which checks if the method has already been called
The sample report ZUS_SDN_METHODS_WITH_MEMORY show how to implement this requirement:
&----
*& Report ZUS_SDN_METHODS_WITH_MEMORY
*&
&----
*& Description: class memorized method calls
&----
*& Link: https:||forums.sdn.sap.com/threa.jspa?messageID=4646956
*&
*&
&----
REPORT zus_sdn_methods_with_memory.
----
CLASS lcl_memory DEFINITION
----
*
----
CLASS lcl_memory DEFINITION.
TYPE-POOLS: abap.
PUBLIC SECTION.
CLASS-METHODS:
static_method_1.
METHODS:
instance_method_1,
instance_method_2,
display.
PRIVATE SECTION.
TYPES:
ty_t_methods TYPE STANDARD TABLE OF tmdir
WITH DEFAULT KEY.
CLASS-DATA:
mt_methods TYPE ty_t_methods.
CLASS-METHODS:
has_been_called
IMPORTING
value(id_method) TYPE seocpdname
RETURNING
value(rd_result) TYPE abap_bool.
DATA:
mo_table TYPE REF TO cl_salv_table.
ENDCLASS. "lcl_memory DEFINITION
----
CLASS lcl_memory IMPLEMENTATION
----
*
----
CLASS lcl_memory IMPLEMENTATION.
METHOD static_method_1.
IF ( has_been_called( 'STATIC_METHOD_1' ) = abap_false ).
ELSE.
ENDIF.
ENDMETHOD. "static_method_1
METHOD instance_method_1.
IF ( has_been_called( 'INSTANCE_METHOD_1' ) = abap_false ).
ELSE.
ENDIF.
ENDMETHOD. "instance_method_1
METHOD instance_method_2.
IF ( has_been_called( 'INSTANCE_METHOD_2' ) = abap_false ).
ELSE.
ENDIF.
ENDMETHOD. "instance_method_2
METHOD display.
TRY.
CALL METHOD cl_salv_table=>factory
EXPORTING
LIST_DISPLAY = IF_SALV_C_BOOL_SAP=>FALSE
R_CONTAINER =
CONTAINER_NAME =
IMPORTING
r_salv_table = me->mo_table
CHANGING
t_table = mt_methods.
CATCH cx_salv_msg .
ENDTRY.
CHECK ( me->mo_table IS BOUND ).
CALL METHOD me->mo_table->display( ).
ENDMETHOD. "display
METHOD has_been_called.
define local data
DATA:
ls_method LIKE LINE OF mt_methods.
READ TABLE mt_methods TRANSPORTING NO FIELDS
WITH KEY table_line = id_method.
BINARY SEARCH.
IF ( syst-subrc = 0 ).
rd_result = abap_true.
ELSE.
rd_result = abap_false.
CLEAR: ls_method.
ls_method-classname = 'LCL_MEMORY'.
DESCRIBE TABLE mt_methods.
ls_method-methodindx = syst-tfill + 1.
ls_method-methodname = id_method.
APPEND ls_method TO mt_methods.
ENDIF.
SORT mt_methods BY table_line.
ENDMETHOD. "has_been_called
ENDCLASS. "lcl_memory IMPLEMENTATION
DATA:
go_memory TYPE REF TO lcl_memory,
go_table TYPE REF TO cl_salv_table.
START-OF-SELECTION.
CREATE OBJECT go_memory.
CALL METHOD go_memory->static_method_1( ).
CALL METHOD go_memory->instance_method_2( ).
CALL METHOD go_memory->instance_method_1( ).
CALL METHOD go_memory->display( ).
END-OF-SELECTION.
Regards,
Uwe