I have some loops inside of each other. In the most inner loop I calculate values that will be needed outside of the loops later.
public class Foo
{
public void Bar()
{
...
double baz;
var i = 0;
while (i < 100)
{
...
var j = 0;
while (j < 100)
{
...
baz = DoSomething(i, j);
j++;
}
i++;
}
// some more calculations. helper1 and helper2 with only the
// the last values of i and j are needed and I do not want
// to calculate them again
}
private double DoSomething(int i, int j)
{
// many lines of code with complex calculations
var helper1 = i + j;
var helper2 = 2*i + j;
baz = helper1 + helper2;
return baz;
}
}
Option 1: I could calculate helper1 and helper2 again but this is time consuming and not necessary.
Option 2: I could pass out the values with out
.
Option 3: I could create a static class with static properties where I store the values in. Inside Loop 2: StaticClass.MoreValues1 = bar;
...
All three options do not seem good practice to me. Is there a better way?
Many thanks
Edit: Updated code block for clarification.
Edit: Added dependency of i and j to helper1 and helper2