0

Ok let me simplify the code logic, We have a C++ project and there are at least 100 functions with similar following pattern. I need to convert it into Java code. Then I have some problem

int evaluateFunc(const para1 /*input only*/, para2 &/* input&output*/, para3 & /*output only */ ...)
{
   int result = OK;
   ...
   // For Input parameter, we defined as const. e.g. para1
   // For Input/Output parameter, we pass either by reference or pointer, as it may get updated during data processing.  e.g. para2 
   // For output only parameter, which will be modified inside function. e.g. para3
   ...
   return result;
}

There are two keys in our evaluateFunc function

  1. the input/output or output only parameters will be updated by our function as after this function call, these data will be processed by some other function as well. so we use either reference or pointer as parameter and pass to each functions.
  2. this function need always return an integer value as result to indicate whether this function success or not. e.g. 0 is success, any other integer is error code. WE have a big error list and it could more than 50 different error code. As we have some error handling outside to check this error code and depend the error code to process it accordingly.

Based on my understanding, Java don't support pass by reference or pointer. And all values are passed by copy. Java only can return one value each time which mean if every function need return both error code + some new data (or modified data), then I need create a class for each of them. Unfortunately, the only common part for each function is error code return is expected, all other value return is random. So in the worst case, I need create 100 different class to cover all the different function return result. Is there any generic solution for Java function return result?

zheng
  • 31
  • 5

1 Answers1

2

Welcome to Java!

Just create a new reuslt object of MyResult type and return!

class MyResult{
    String strValue;
    boolean result;

    MyResult(String strValue, boolean result){
        this.strValue = strValue;
        this.result = result;
    }

    boolean getResult(){
        return result;
    }

    boolean getStrValue(){
        return strValue;
    }
}

In your code,

MyResult evaluateSring(String strValue)
{
    //some process of strvalue and 
   bool result = false;
   strValue = strValue.strstr( x, y)
   ...
   return new MyResult(strValue, result);
}

At he place you are calling this method, do

MyResult myResult =  evaluateSring(strValue)

strValue = myResult.getStrValue();
boolean result = myResult.getResult();
Tejas jain
  • 742
  • 7
  • 20
  • Thanks, Wow Java looks so complex. So in order to implement my funciotn, i need create new class include two value which i want to return. For one function is ok. but i have more than 100 function need change, and each function have different return type and value. which mean i need create 100 class in order to support return data feature? Wow. Java definitely a new world to me – zheng Apr 22 '23 at 20:29
  • @zheng You won't have to implement 100 classes to hold results. You should edit your question with a better/working Java example (possibly two) and the results. Do you need the updated return value if result is false? – DuncG Apr 22 '23 at 22:06
  • Hi DuncG, thank, the current c++ code have many function which need return multiple result. and most function looks like this: – zheng Apr 23 '23 at 06:46
  • This code (specifically `MyResult`) can be significantly shortened by using `public record MyResult(String strValue, boolean result) { }`. Instead of `getStrValue()` and `getResult()`, you then automatically get the accessors `strValue()` and `result()`. – Mark Rotteveel Apr 23 '23 at 11:42