1

I have two inputfields at my visual force page to insert the B_value_per_Month and the B_Birthdate, after the calucaltion you will get the B_result. For the calculation I need the time between the birthdate and the day of today, so far so good, but for each year (e.g. 3 years = 0.6) it's an other factor.

In my method PageReference B_Calc_click() I run the method getB_runtime(), but how can I put the the B_runtime into the method get_B_runtime_facor()?

public void getB_runtime_factor() {
    B_runtime_factor_Map = new Map <Double, Double>();
    B_runtime_factor_Map.put(0, 1.000);
    B_runtime_factor_Map.put(1, 0.800);
    B_runtime_factor_Map.put(2, 0.700);
    B_runtime_factor_Map.put(3, 0.600);
    B_runtime_factor_Map.put(4, 0.500);
    B_runtime_factor_Map.put(5, 0.400);
    ......
}

public PageReference B_CALC_click() {
    B_runtime = Date.Today().daysBetween(B_Birthdate) / 365;
    getB_runtime_factor();      
    B_Result = B_Value_per_Month * 12 * B_runtime_factor;       
    return new PageReference('/apex/Parameter_info');
}

2 Answers 2

1

Martin's answer is correct, but I would refactor this to use static initialization to avoid having to call a method to initialize the map:

public Map<Double, Double> B_runtime_factor = new Map<Double, Double> {
    0 => 1.000, 
    1 => .800, 
    2 => .700, 
    ...
};

public PageReference B_CALC_click() {
    B_runtime = Date.Today().daysBetween(B_Birthdate) / 365;
    B_Result = B_Value_per_Month * 12 * B_runtime_factor.get(B_runtime);       
    return new PageReference('/apex/Parameter_info');  
}    
4

I hope I understand well your question.

First, I think there's a problem with the name of your getB_runtime_factor() method. It should be called something like: initializeB_runtime_factor(). Because this doesn't return anything. And you won't need it to return the factor. Because your map is accessible in your B_CALC_click() method. To access a Map value using a key, you can use the get() method. Also, you can check if the map contains the factor using the containsKey() method. Here's the code for your case:

public void initializeB_runtime_factor() {
    B_runtime_factor_Map = new Map <Double, Double>();
    B_runtime_factor_Map.put(0, 1.000);
    B_runtime_factor_Map.put(1, 0.800);
    B_runtime_factor_Map.put(2, 0.700);
    B_runtime_factor_Map.put(3, 0.600);
    B_runtime_factor_Map.put(4, 0.500);
    B_runtime_factor_Map.put(5, 0.400);
    ......
}

public PageReference B_CALC_click() {
    B_runtime = Date.Today().daysBetween(B_Birthdate) / 365;
    initializeB_runtime_factor();
    if(B_runtime_factor_Map.containsKey(B_runtime)){
        B_Result = B_Value_per_Month * 12 * B_runtime_factor_Map.get(B_runtime);       
        return new PageReference('/apex/Parameter_info');
    }else{
        //execute what you want when the key is not in the map
    }

    return null;
}
2
  • If I save the code I get the message from the developer console "Non-void method might not return a value or might have statement after a return statement", could you help?
    – peXeq
    Mar 22, 2017 at 7:53
  • I updated my answer. You needed to add a return null at the end of the B_CALC_click() method. Because you're supposed to return a PageReference. Mar 22, 2017 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.