title: Need to calculate average of multiple TimeSpan values entered by user
body: <p>This is a running pace/time calculator I'm working on. This particular problem I'm having is finding the average pace for entered mile time values. I have the user enter multiple values in hh:mm:ss format via text boxes. I need to add the values together, find the average of the values entered, and return that average value to a label in hh:mm:ss format. I've tried various ways of doing this and read multiple other posts but keep getting stuck.</p>
  
  <ul>
  <li><p>I've tried using List but the number of values entered always varies, I need it to be dynamic. If there's a way to only enter the user-defined values into the list, my list would work. As of now, it gets the average of all my boxes even if one of them doesn't have a value.</p></li>
  <li><p>Also tried just adding the TimeSpan values together and divide them by a count. This gets the correct count for how many values were entered by the user. However, I get an error saying cannot apply the / operator to operands of type TimeSpan and int.</p></li>
  </ul>
  
  <p>This is not the same scenario as Find average of collection of TimeSpans. The code used in that article broke my entire app.</p>
  
  <pre><code>        private void calculate_btn_Click(object sender, EventArgs e)
          {
              TimeSpan d1 = TimeSpan.Parse(textBox1.Text);
              TimeSpan d2 = TimeSpan.Parse(textBox2.Text);
              TimeSpan d3 = TimeSpan.Parse(textBox3.Text);
  
              var sourceList = new List&lt;TimeSpan&gt;();
  
              sourceList.Add(d1);
              sourceList.Add(d2);
              sourceList.Add(d3);
  
              var averageTimeSpan = new TimeSpan(Convert.ToInt64(sourceList.Average(timeSpan =&gt; timeSpan.Ticks)));
              averagelabletext.Text = averageTimeSpan.ToString();
          }
  
  </code></pre>
  
  <pre><code>    private void calculate_btn_Click(object sender, EventArgs e)
      {
          TimeSpan d1 = TimeSpan.Parse(textBox1.Text);
          TimeSpan d2 = TimeSpan.Parse(textBox2.Text);
          TimeSpan d3 = TimeSpan.Parse(textBox3.Text);
  
          TimeSpan total = (d1 + d2 + d3);
  
          int count = 0;
  
          foreach (Control c in Controls)
          {
              if (!(c is TextBox)) continue;
              TextBox t = c as TextBox;
              {
                  if (t.Text != "00:00:00")
                  {
                      count++;
                  }
              }
          }
  
          averagelabletext.Text = total / count;
      }
  </code></pre>
  
  <p>I expect if a user enters 00:09:00 into textbox1 and 00:09:30 into textbox 2 the result should be 00:09:15.</p>
  
  <p>Right now, if a user enters those same values, the average returned is 00:06:10 since it's still calculating that third value into the average. Since the user hasn't entered a value into that box, it's still 00:00:00.</p>