Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to use class object from one method to another in class

former_member383962
Participant
0 Kudos

Hello Experts,

I created a class with two methods using SE24.

Methods:

1. Get Data

2. Process Data

In a Get Data method i wrote a below code:


* References declarations
  DATA:
        lref_excel                                     TYPE REF TO zkab_cl_fdt_xl_spreadsheet,
        lref_excel_core                             TYPE REF TO cx_fdt_excel_core.

  DATA: lv_msg type string.

*  * * Internal tables declaration
  DATA:
        lt_worksheet                             TYPE STANDARD TABLE OF STRING,
        lw_worksheets like line of lt_worksheet.

  DATA: wa_worksheet type zkab_st_worksheet.

    TRY.

*     Create object of class to read .xlsx file contents

      CREATE OBJECT lref_excel
        EXPORTING
           document_name = file_name
           xdocument     = file_content.

    CATCH cx_fdt_excel_core INTO lref_excel_core.
      CLEAR lv_msg.

*     Call method to get error message text

      CALL METHOD lref_excel_core->if_message~get_text
        RECEIVING
          RESULT = lv_msg.
*<< Display error message returned in lv_msg >>
      RETURN.
  ENDTRY.

* Call method to get list of worksheets in the .xlsx file

  lref_excel->if_fdt_doc_spreadsheet~get_worksheet_names(
      IMPORTING
        worksheet_names = lt_worksheet ).

from the above code, i created a object called "lref_excel".

Now, i want to use the same object in a "Process data" method. is it possible....??

Because, in a "Process data" method also use the same logic what i wrote in a first method with some extended logics to processing the data.

Any suggestions please...

Thanks in advance...

7 REPLIES 7

Patrick_vN
Active Contributor

I guess you could create mref_excel as an attribute of your class instead of the local variable lref_excel (if the get_data & process_data are instance methods). If not, you have the option of using respectively an exporting- and an importing parameter.

0 Kudos

I believe, Creating a global attribute in class is a better solution then using ref in export import parameters. Preferably create a Private attribute in class is more meaningful in his case.

0 Kudos

Even if the get_data- & process_data-methods are static methods?

0 Kudos

s1252 why do you think so?
I'd prefer to pass the objet, maybe as return parameter.

0 Kudos

Hello s1252

Thanks for your response...

I can create lref as an attribute.. but how to pass that exporting parameters to that attribute..

i can't get it...

can you provide some sample codes...

thanks in advance...

0 Kudos

If those are static methods , then we better go for import export parameters as Patrick Van Nierop suggested and add them in method signature exporting/returning parameters .

former_member557605
Participant
0 Kudos

Hi Kabilarasan,

If your methods are static, then you can directly call that method by class name.

Click on pattern, and

  1. select ABAP object pattern - press OK
  2. give class name and method name - press OK

Regards,

Rupesh