Search code examples
c#windowsformssharpdevelop

How to fix "the name ... does not exist in the current context error" in a C# Windows Application?


I made a simple C# app some time ago (as a console app), now I'm making a new version as a Windows Application in Sharpdevelop. I'm pretty new to C# and graphical applications and I got this confusing error.

What I want to do: on the UI, I have a button which checks if COM3 serial port is opened and if not, open it, if it's open, close it and write a message about it on a listbox. The problem is that wherever I put the serial handling part, I get errors about it not being in the current context, or get an "Object reference not set to an instance of an object" error.

I created the app with Sharpdevelop's template, so I have code in several files:

Program.cs:

using System;
using System.Windows.Forms;
namespace SerialClient_v2
{
    class Program
    {   
        [STAThread]

        void Main(string[] args)
        {
            SerialHandle SH=new SerialHandle();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm(SH));
        }       
    }   
}

Mainform.cs:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;

namespace SerialClient_v2
{
    public partial class MainForm : Form
    {   
        private SerialHandle _sh;
        public MainForm(SerialHandle sh)
        {
            InitializeComponent();
            _sh = sh;

        }
        void ConnectBtnClick(object sender, EventArgs e)
        {
            try{
                if(_sh._serialPort.IsOpen){
                    listBox1.Items.Add(DictionaryClass.strDisconnecting);
                    //Program._serialPort.Close();
                }
                else{
                    //Program._serialPort.Open();
                    listBox1.Items.Add(DictionaryClass.strConnecting);
                }
            }
            catch (Exception ex)  
            {  
                listBox1.Items.Add("Error opening/writing to serial port :: " + ex.Message);  
            }
        }
    }
}

SerialHandle.cs: (this is my file, the stuff which used to be in the Main function in the console app comes here. DictionaryClass is just a translation helping class to easily switch between two languages)

using System;
using System.IO.Ports;

namespace SerialClient_v2
{
    public class SerialHandle
    {
        bool SerialComm;  
        public SerialPort _serialPort;



        public SerialHandle()
        {
            SerialPort _serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One); 
            _serialPort.Handshake = Handshake.None; 

            _serialPort.ReadTimeout = 1000;   

            _serialPort.Open();
        }

    }

    public static class DictionaryClass{
        //DICTIONARY:
        public static string  strConnecting="Initializing serial communication!";
        public static string  strDisconnecting="Closing serial port";
    }
}

The problem is that SH._serialPort.IsOpen can't be checked as it's not in the context. SH is created in the Main function, so I don't get why it's not seen. Sorry for the beginner question.


Solution

  • The simplest solution is to pass the SH object instance to the main form:

    In SerialHandle.cs (use _sh._serialPort.IsOpen):

    private SerialHandle _sh;
    
    public MainForm(SerialHandle sh)
    {
        _sh = sh;
    }
    

    In Program.cs:

    Application.Run(new MainForm(SH));