title: C# - Calculations as objects - Many Calculations with Many Dependencies
body: <p>I have developed a fairly complex spreadsheet in Excel, and I am tasked with converting it to a C# program.</p>
  
  <p>What I am trying to figure out is how to represent the calculations from my spreadsheet in C#.</p>
  
  <p>The calculations have many dependencies, to the point that it would almost appear to be a web, rather than a nice neat hierarchy.</p>
  
  <p>The design solution I can think of is this:</p>
  
  <ul>
  <li><strong>Create an object to represent each calculation.</strong></li>
  <li><p><strong>Each object has an integer or double, which contains the calculation.</strong>
  this calc has inputs from other objects and so requires that they are evaluated first before it can be performed.</p></li>
  <li><p><strong>Each object has a second integer "completed"</strong>, which evaluates to 1 if the previous calculation is successful</p></li>
  <li><strong>Each object has a third integer "ready"</strong> 
  This item requires all precedent object's "completed" integers evaluate to
  "1" and if not, the loop skips this object</li>
  <li><strong>A Loop runs through all objects, until all of the "completed" integers = 1</strong></li>
  </ul>
  
  <p>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.</p>
  
  <p>I appreciate any help with this issue, and I'm keen to hear your thoughts! :)</p>
  
  <p>edit*</p>
  
  <p>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?</p>
  
  <p>I have it set to "Any CPU", the default setting.</p>
  
  <p>edit*</p>
  
  <p>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)"</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>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</p>