Hi all,
I'm currently building a semi-complicated calculator which is basically a conversion from an Excel spreadsheet I've been provided.
I've nailed most of it but there's a part in the Excel spreadsheet where multiple calculations occur between 6 rows and 7 columns, but the issue is that the calculations happen in no particular order what-so-ever.
So for example, Row0[Column1]
is calculated using (Row2[Column4] * Row2[Column5])
and Row1[Column4]
is calculated using (Row4[Column2] / Row5[Column1])
and so forth.. you get the idea.
I've thought about using a 2D array, but am afraid that the values will calculate in a particular order, thus having no value when they are reached. As far as I'm aware, Row1 will be calculated first, then Row2, Row3, etc.
So, without creating a variable for each cell in my excel spreadsheet (and ordering it appropriately), is there a way I can calculate this using C#?
I would really appreciate any help, advice, pointers, whatever you think may be possible - I'd love to hear it!
EDIT After implementing the Lazy class provided by @dtb, I've got the following code. It's a straight copy of what's in the Excel spreadsheet I've been provided, including pointers & calculations.
var sr = new Lazy<decimal>[6, 6];
sr[0, 0] = new Lazy<decimal>(() => sr[1, 0].Value - eNumber);
sr[0, 3] = new Lazy<decimal>(() => sr[0, 4].Value - sr[1, 0].Value - sr[1, 4].Value);
sr[0, 4] = new Lazy<decimal>(() => sr[0, 0].Value * edD);
sr[0, 5] = new Lazy<decimal>(() => sr[0, 0].Value);
sr[1, 0] = new Lazy<decimal>(() => sr[1, 5].Value);
sr[1, 4] = new Lazy<decimal>(() => sr[1, 0].Value * edD);
sr[1, 5] = new Lazy<decimal>(() => sr[2, 0].Value + sr[2, 5].Value);
sr[2, 0] = new Lazy<decimal>(() => eNumber * rRate);
sr[2, 4] = new Lazy<decimal>(() => sr[2, 0].Value * hdD);
sr[2, 5] = new Lazy<decimal>(() => sr[1, 5].Value);
sr[3, 1] = new Lazy<decimal>(() => sr[2, 5].Value);
sr[4, 2] = new Lazy<decimal>(() => eNumber * (ePc / 100) + sr[2, 0].Value * (hlPc / 100) - sr[3, 1].Value);
sr[5, 0] = new Lazy<decimal>(() => (sr[0, 0].Value + sr[1, 0].Value + sr[2, 0].Value) / ePerR);
sr[5, 2] = new Lazy<decimal>(() => sr[5, 0].Value / rLifecycle);
sr[5, 4] = new Lazy<decimal>(() => sr[5, 2].Value);
sr[5, 5] = new Lazy<decimal>(() => sr[5, 0].Value + sr[5, 2].Value - sr[5, 4].Value);
However I get the following error
ValueFactory attempted to access the Value property of this instance.
Googling the error has returned a bunch of spammy search type websites.
Marko