Hello Jeffrey,
I routinely implement iterators for internal tables using local classes like
CLASS lcl_iterator DEFINITION FRIENDS lif_unit_test.
PUBLICSECTION.
METHODS constructor IMPORTING it_messages TYPE tt_trace
iv_start TYPE sytabix DEFAULT1
iv_stop TYPE sytabix OPTIONAL.
METHODSnext RETURNING VALUE(rs_data)TYPE ts_message
RAISING cx_sy_itab_error.
METHODS has_next RETURNING VALUE(rv_flag)TYPE xsdboolean.
METHODSskipIMPORTING iv_count TYPE i DEFAULT1.
PROTECTEDSECTION.
DATA mv_idx TYPE sytabix.
DATA mv_size TYPE sytabix.
DATA mt_list TYPE tt_trace.
ENDCLASS.
A use case is the event handler of ALV Grid: I pass an iterator and the handler processes selected entries without having to know about the selection or additional validation logic. It is just a case of separation of concern.
CLASS lcl_iterator IMPLEMENTATION.
METHOD constructor.
CLEAR mv_idx.
mt_list = it_messages. " shared, never changed
mv_size = COND #(WHEN iv_stop ISINITIAL THEN lines( mt_list )
ELSE iv_stop ).
skip( iv_start - 1).
ENDMETHOD.
METHODnext.
skip().
rs_data = mt_list[ mv_idx ].
ENDMETHOD.
METHOD has_next.
rv_flag = boolc( mv_idx < mv_size ).
ENDMETHOD.
METHODskip.
ADD iv_count TO mv_idx.
ENDMETHOD.
ENDCLASS.
Reminder: a new object must be created for each iteration. Access to the table index MV_IDX should be avoided. But if worst comes to the worst...
this ABAP criminal will avoid ABAP police infested blogs
regards,
JNN