C#, member and variable scope
<p>Relates to the problem <a href="http://stackoverflow.com/questions/9005603/adding-event-handler-in-main-for-serialport">Adding event handler in main() for SerialPort </a></p>

<p>After I change the mycomport to one of the member of the class, why I cant use it in main?</p>

<p><img src="http://img11.imageshack.us/img11/3664/49512217.png" alt="a busy cat"></p>

<pre><code>namespace serialport
{
public class Program
{


    #region Manager Variables
    //property variables
    private string _baudRate = string.Empty;
    private string _parity = string.Empty;
    private string _stopBits = string.Empty;
    private string _dataBits = string.Empty;
    private string _portName = string.Empty;
    private RichTextBox _displayWindow;
    //global manager variables
    //private Color[] MessageColor = { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };
    internal List&lt;Byte&gt; portBuffer = new List&lt;Byte&gt;(1024);
    private SerialPort myComPort = new SerialPort();
    #endregion



    #region Manager Constructors
    /// &lt;summary&gt;
    /// Comstructor to set the properties of our
    /// serial port communicator to nothing
    /// &lt;/summary&gt;
    public Program()
    {
        _baudRate = string.Empty;
        _parity = string.Empty;
        _stopBits = string.Empty;
        _dataBits = string.Empty;
        _portName = "COM1";
        _displayWindow = null;
        //add event handler
        myComPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
    }
    #endregion

    static void Main()
    {

        Program myProgram = new Program();
        //1. find available COM port
        string[] nameArray = null;
        string myComPortName = null;
        nameArray = SerialPort.GetPortNames();
        if (nameArray.GetUpperBound(0) &gt;= 0)
        {
            myComPortName = nameArray[0];
        }
        else
        {
            Console.WriteLine("Error");
            return;
        }


        //2. create a serialport object
        // the port object is closed automatically by use using()
        myComPort.PortName = myComPortName;
        //the default paramit are 9600,no parity,one stop bit, and no flow control



        //3.open the port
        try
        {
            myComPort.Open();
        }
        catch (UnauthorizedAccessException ex)
        {
            MessageBox.Show(ex.Message);
        }
        //Add timeout, p161

        //reading Bytes
        byte[] byteBuffer = new byte[10];
        Int32 count;
        Int32 numberOfReceivedBytes;
        myComPort.Read(byteBuffer, 0, 9);
        for (count = 0; count &lt;= 3; count++)
        {
            Console.WriteLine(byteBuffer[count].ToString());
        }


    }
    //The event handler should be static??
    internal void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        int numberOfBytesToRead;
        numberOfBytesToRead = myComPort.BytesToRead;
        byte[] newReceivedData = new byte[numberOfBytesToRead];
        myComPort.Read(newReceivedData, 0, numberOfBytesToRead);
        portBuffer.AddRange(newReceivedData);
        ProcessData();
    }
    private void ProcessData()
    {
        //when 8 bytes have arrived, display then and remove them from the buffer
        int count;
        int numberOfBytesToRead = 8;

        if (portBuffer.Count &gt;= numberOfBytesToRead)
        {
            for (count = 0; count &lt; numberOfBytesToRead; count++)
            {
                Console.WriteLine((char)(portBuffer[count]));
            }
            portBuffer.RemoveRange(0, numberOfBytesToRead);
        }
    }

}
</code></pre>

<p>}</p>
