Application Development and Automation 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: 
Read only

Getting value from program/method in innermost method using STATIC VARIABLE and classes

sanjay_deshpande4
Participant
0 Likes
3,136

Dear All,

I have a requirement of passing the value form custom method inside main program into another custom method inside the method with value(nested custom methods) .I am not supposed to add any import/export/changing parameter to Method interface. No global values available inside innermost method.I came to know that I can use static variable along with classes for the requirement .However I do not know how to achieve it.

Can anybody kindly guide with little bit of code on how to achieve this.

Regards.

1 ACCEPTED SOLUTION
Read only

Gourab_Dey
Product and Topic Expert
Product and Topic Expert
0 Likes
2,999

This can be done by following steps:

  • Define an attribute in the class which will hold the value and will transfer to the second method. You can create the variable in the same class or you can create a new class that will act as a container class and will transfer to another class. Please do remember to make the attribute as "static attreibute" and it should be public. I will prefer to create a separate class to transfer the value(refer below screenshot)

  • Next, set the value of this attribute in the method from which value will be transported to the other inner method. It can be done in the following way.
ZCL_TEST_CONTAINER=>MV_VALUE = 'Hello World!'.
  • No the value is stored. No go to the method where you want to get the value. You can get the value in your inner method as follows
DATA: lv_string type string.
lv_string = zcl_test_container=>mv_value.

Now you can see the value of the lv_string is 'Hello World!'. You have successfully passed the value from one method to other without using importing/exporting or parameters.

Note: The attribute should be static, else it will not work. That's the key.

Cheers!

Gourab

5 REPLIES 5
Read only

matt
Active Contributor
2,999

I've not much of an idea of what you're doing. But let me say this:

You do not have a requirement of passing the value.... etc. You have a goal to achieve. You or someone else has suggested that this is a way. If it isn't you, then go back to that person for clarification.

If it is you, then tell us the actual requirement. What are you wanting to do? If it's enhancing a particular standard SAP method, then which one? If it's all custom, then how, exactly, have you designed the class?

Read only

Gourab_Dey
Product and Topic Expert
Product and Topic Expert
0 Likes
3,000

This can be done by following steps:

  • Define an attribute in the class which will hold the value and will transfer to the second method. You can create the variable in the same class or you can create a new class that will act as a container class and will transfer to another class. Please do remember to make the attribute as "static attreibute" and it should be public. I will prefer to create a separate class to transfer the value(refer below screenshot)

  • Next, set the value of this attribute in the method from which value will be transported to the other inner method. It can be done in the following way.
ZCL_TEST_CONTAINER=>MV_VALUE = 'Hello World!'.
  • No the value is stored. No go to the method where you want to get the value. You can get the value in your inner method as follows
DATA: lv_string type string.
lv_string = zcl_test_container=>mv_value.

Now you can see the value of the lv_string is 'Hello World!'. You have successfully passed the value from one method to other without using importing/exporting or parameters.

Note: The attribute should be static, else it will not work. That's the key.

Cheers!

Gourab

Read only

sanjay_deshpande4
Participant
2,999

Great Gaurav. Thanks a lot for helping.

This has worked and I was looking for this solution only.

Read only

2,999

Please use the COMMENT button for comments, questions, adding details, replying to a comment or a proposed solution or to the OP question, etc., ANSWER is only to propose a solution, dixit SAP text at the right of the answer area.

Read only

Sandra_Rossi
Active Contributor
2,999