I have developed a fairly complex spreadsheet in Excel, and I am tasked with converting it to a C# program.
What I am trying to figure out is how to represent the calculations from my spreadsheet in C#.
The calculations have many dependencies, to the point that it would almost appear to be a web, rather than a nice neat hierarchy.
The design solution I can think of is this:
- Create an object to represent each calculation.
Each object has an integer or double, which contains the calculation. this calc has inputs from other objects and so requires that they are evaluated first before it can be performed.
Each object has a second integer "completed", which evaluates to 1 if the previous calculation is successful
- Each object has a third integer "ready" This item requires all precedent object's "completed" integers evaluate to "1" and if not, the loop skips this object
- A Loop runs through all objects, until all of the "completed" integers = 1
I hope this makes sense. I am typing up the code for this but I am still pretty green with C# so at least knowing i'm on the right track is a boon :) To clarify, this is a design query, I'm simply looking for someone more experienced with C# than myself, to verify that my method is sensible.
I appreciate any help with this issue, and I'm keen to hear your thoughts! :)
edit*
I believe the "completed" state and "ready" state are required for the loop state check to prevent errors that might occur from attempts to evaluate a calculation where precedents aren't evaluated. Is this necessary?
I have it set to "Any CPU", the default setting.
edit*
For example, one object would be a line "V_dist" It has length, as a property. It's length "V_dist.calc_formula" is calculated from two other objects "hpc*Tan(dang)"
public class inputs
{
public string input_name;
public int input_angle;
public int input_length;
}
public class calculations
{
public string calc_name; ///calculation name
public string calc_formula; ///this is just a string containing formula
public double calculationdoub; ///this is the calculation
public int completed; ///this will be set to 1 when "calculationdoub" is nonzero
public int ready; ///this will be set to 1 when dependent object's "completed" property = 1
}
public class Program
{
public static void Main()
{
///Horizontal Length
inputs hpc = new inputs();
hpc.input_name = "Horizontal "P" Length";
hpc.input_angle = 0;
hpc.input_length = 200000;
///Discharge Angle
inputs dang = new inputs();
dang.input_name = "Discharge Angle";
dang.input_angle = 12;
dang.input_length = 0;
///First calculation object
calculations V_dist = new calculations();
V_dist.calc_name = "Vertical distance using discharge angle";
V_dist.calc_formula = "hpc*Tan(dang)";
**V_dist.calculationdoub = inputs.hpc.length * Math.Tan(inputs.dang.input_angle);**
V_dist.completed = 0;
V_dist.ready = 0;
}
}
It should be noted that the other features I have yet to add, such as the loop, and the logic controlling the two boolean properties