title: C# - Tree / Recursion in Get and Set Accessors?
body: <p>I have a tree (a <code>List&lt;T&gt;</code>) that contains a number of <code>ItemType</code> classes (see code below); the class has the properties <code>OverrideDiscount</code> (which could be <code>null</code>, indicating to use <code>DefaultDiscount</code> (which could be <code>null</code>, indicating to use the <strong>parent</strong> <code>ItemType</code>'s <code>CalculatedDiscount</code>))</p>
  
  <p>So you see I need to recurse up the tree (which incidentally is a <code>List&lt;ItemType&gt;</code>) to get the parent's <code>CalculatedDiscount</code>, because that could be <code>null</code>, which means you need to get the parent's parent's <code>CalculatedDiscount</code> and so on...</p>
  
  <p>Is it a bad idea to put the code for this in the <code>Get</code> accessor? </p>
  
  <p>How would you handle it?</p>
  
  <p>Just as a sidenote, all this data comes via an <code>SqlDataReader</code> from a database in no particular order, then after that the <code>Children</code> property list is populated by looping through the tree and adding to the <code>Children</code> list as appropriate. So the parents are unaware of the children until AFTER the <code>Set</code> accessor has been called, ruling out putting anything useful in the <code>Set</code> accessor (e.g. setting all children's <code>CalculatedDiscount</code> in the <code>Set</code> accessor). Unless I've missed some other way of doing it (very possible, recursion fries my brain sometimes).</p>
  
  <p>Thanks in advance</p>
  
  <p>The class so far:</p>
  
  <pre><code>    public class ItemType
      {
          public int ID;
          public int? ParentID;
          public List&lt;ItemType&gt; Children;
  
          public double? DefaultDiscount; 
          public double? OverrideDiscount; 
          public double CalculatedDiscount
          {
              get
              {
                  if (OverrideDiscount != null)
                  {
                      return (double)OverrideDiscount; //+ Autospec qty
                  }
                  else
                  {
                      if (DefaultDiscount != null)
                      {
                          return (double)DefaultDiscount;
                      }
                      else
                      {
  
                          //I need to get this ItemType's parent's discount 
                          //here by recursing up the tree...is this a bad idea?
                      }
                  }
              }
          }
      }
  </code></pre>