title: Error in C#: "An object reference is required for the non-static field, method, or property"
body: <p>I wrote code in WPF. Firstly, I wrote a separate project to test work with a <a href="http://en.wikipedia.org/wiki/COM_%28hardware_interface%29" rel="noreferrer">COM port</a> device, and it worked well. Next I decided to integrate it in another project, but I get an error. I didn't change the code; I am just copied it into a new code file.</p>
  
  <p>This code works well:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using System.IO.Ports;
  using System.Windows.Threading;
  
  namespace WpfApplication2
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              serial.BaudRate = 9600;
              serial.Handshake = System.IO.Ports.Handshake.None;
              serial.Parity = Parity.None;
              serial.DataBits = 8;
              serial.StopBits = StopBits.One;
              serial.ReadTimeout = 200;
              serial.WriteTimeout = 500;
              serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
          }
  
          SerialPort serial = new SerialPort();
          private string recieved_data;
  
          private delegate void UpdateUiTextDelegate(string text);
  
          private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
          {
              if (serial.IsOpen)
              {
                  try
                  {
                      recieved_data = serial.ReadLine();
                      Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), recieved_data);
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
              }
          }
  
          private void DisplayText(string code)
          {
              textBox1.AppendText(string1);
          }
  
          private void button1_Click(object sender, RoutedEventArgs e)
          {
              ListBoxItem lbi = new ListBoxItem();
              lbi = (ListBoxItem)listBox1.SelectedItem;
              serial.Close();
              serial.PortName = "COM" + (string)lbi.Content;
              try
              {
                  serial.Open();
                  textBox1.AppendText("Device opened at " + serial.PortName + '\n');
              }
              catch (Exception ex)
              {
                  textBox1.AppendText(ex.Message + '\n');
              }
          }
      }
  }
  </code></pre>
  
  <p>But this one doesn't want to work, and I can't understand why:</p>
  
  <pre><code>using System.IO.Ports;
  using System.Windows.Threading;
  using System;
  using System.Windows;
  
  namespace PresidentProtocol
  {
      public class QRBarCode
      {
         // private SerialPort serial = new SerialPort();
  
          public QRBarCode(string com)
          {
              serial.BaudRate = 9600;
              serial.Handshake = System.IO.Ports.Handshake.None;
              serial.Parity = Parity.None;
              serial.DataBits = 8;
              serial.StopBits = StopBits.One;
              serial.ReadTimeout = 200;
              serial.WriteTimeout = 500;
              serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve);
              serial.Close();
              serial.PortName = com;
              try
              {
                  serial.Open();
              }
              catch (Exception)
              {
                  MessageBox.Show("Error opening COM port.");
              }
          }
  
          SerialPort serial = new SerialPort();
          private string recieved_data;
  
  
          private delegate void UpdateUiTextDelegate(string text);
  
          private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
          {
              if (serial.IsOpen)
              {
                  try
                  {
                      recieved_data = serial.ReadLine();
                      Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), recieved_data);
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
              }
          }
  
          private void DisplayText(string code)
          {
              MessageBox.Show(code);
          }
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>An object reference is required for the non-static field, method, or property 'System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority, System.Delegate, object)'
    E:\C#\PresidentProtocol\PresidentProtocol\classes\QRCodeReader.cs</p>
  </blockquote>
  
  <p>on this line of code:</p>
  
  <pre><code>Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), recieved_data);
  </code></pre>