title: Delegates to Pass Data Between Windows C# WPF
body: <p>I am attempting to use <code>Delegates</code> to pass data between two <code>Windows</code>. I want a list of <code>People</code> to appear when the user clicks a button on the main <code>Window</code>, this will then allow the user to select a <code>Person</code>. After doing this it will fill in a <code>TextBox</code> on the original form with the <code>Person</code> name. </p>
  
  <p>I started off much simpler than this however to get my head around <code>Delegates</code> and I'm having issues understanding how to call methods on separate <code>Windows</code> with the <code>Delegates</code>. This is what I have attempted so far;</p>
  
  <p>The method on the main <code>Window</code> that waits for a <code>string</code> (ShowMessage);</p>
  
  <pre><code>public static DelegateTestWindow.TestDelegate ShowDelegateMessage(string message)
  {
      MessageBox.Show(message);
      return null;
  }
  </code></pre>
  
  <p>The second <code>Window</code> that contains the <code>Delegate</code>;</p>
  
  <pre><code>public partial class DelegateTestWindow : Window
  {
      public delegate string TestDelegate();
      public DelegateTestWindow()
      {
          InitializeComponent();
      }
  
      private void TestDelegateClick(object sender, RoutedEventArgs e)
      {
          var tDelegate = new TestDelegate(CompanyManagement.ShowDelegateMessage("Test"));
          this.Close();
      }
  }
  </code></pre>
  
  <p>This does show a message saying "Test" however it also throws an <code>Exception</code> after doing so: <code>Delegate to an instance method cannot have null 'this'.</code></p>
  
  <p>The method on the <code>MainWindow</code> especially does not look correct to me, however after trying different options this is the closest I've got to a working <code>Delegate</code>. Could someone explain where I have (clearly) gone wrong here and how I can improve the system I've got now?</p>