title: I want to eliminate all of these case statements (too messy)
body: <p>I am writing a test program which returns a string &quot;P3-PASS&quot; or &quot;P3-FAIL. In all there are 12 possible tests, P3 to P14 (&quot;P3-FAIL&quot; to &quot;P14-PASS&quot;).</p>
  <p>I have a button &quot;All_Tests&quot; which calls each test 1 by 1, and the associated button changes colour based on the result.</p>
  <p>Ideally I want to do something like PageNum.Background = Brushes.Red, but I can't do this because I can't use a string to access the button. Hence the case statements below. Is there a way to simplify this, because it looks awful. OR is there a way to access the relevant button using a string, or similar. Many Thanks</p>
  <pre><code>int PageNum = Int32.Parse(PageTstName);
  
  switch (PageNum)
  {
      case 3:
          if (TstResult == &quot;PASS&quot;)
          {
              Pg3.Background = Brushes.SeaGreen;
              Pg3.Foreground = Brushes.White;
          }
          else // TstResult is &quot;FAIL&quot;
          {
              Pg3.Background = Brushes.Red;
              Pg3.Foreground = Brushes.White;
          }
          break;
  
      case 4:
          if (TstResult == &quot;PASS&quot;)
          {
              Pg4.Background = Brushes.SeaGreen;
              Pg4.Foreground = Brushes.White;
          }
          else // TstResult is &quot;FAIL&quot;
          {
              Pg4.Background = Brushes.Red;
              Pg4.Foreground = Brushes.White;
          }
          break;
  
      case 5: .....etc
  </code></pre>