title: Access MainWindow variable from class
body: <p>I'm making a WPF application to simulate traffic. I want the <code>Car</code>s to have a reaction delay of 1 second befor changing their acceleration, without stopping the whole application. To do so, I want to acces the <code>elapsed</code> variable from my <code>Car</code> class. The <code>elapsed</code> variable stores how much time has passed. </p>
  
  <p>The code in <code>MainWindow</code>:</p>
  
  <pre><code>namespace TrafficTester
  {
      public partial class MainWindow : Window
      {
          Timer timer = new Timer();
  
  public MainWindow()
      {
          InitializeComponent();
          //create the timer
          timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
          timer.Interval = timerInterval;
          timer.Enabled = true;
  
          //...
  
          void OnTimedEvent(object source, ElapsedEventArgs e)
          {
              timer.Enabled = false; //stop timer whilst updating, so updating won't be called again before it's finished
              update(); //
              timer.Enabled = true;
              elapsed += timerInterval;
          }
      }
  }
  </code></pre>
  
  <p>The code in the <code>Car</code> class:</p>
  
  <pre><code>namespace TrafficTester
  {
      public class Car
      {
  
      //...
  
          public void changeAccel(double val)
          {
              int time = MainWindow.elapsed;
              int stop = MainWindow.elapsed + reactDelay;
              while (time &lt; stop)
              {
                  time = MainWindow.elapsed;
              }
              accel = val;
          }
      }
  }
  </code></pre>
  
  <p><code>accel</code> is the current acceleration and <code>val</code> is the new acceleration. <code>MainWindow.elapsed</code> should call the <code>elapsed</code> variable from MainWindow, but it doesn't. How can I call it from the <code>Car</code> class?</p>