[英]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.前段时间我制作了一个简单的 C# 应用程序(作为控制台应用程序),现在我正在制作一个新版本作为 Sharpdevelop 中的 Windows 应用程序。 I'm pretty new to C# and graphical applications and I got this confusing error.
我对 C# 和图形应用程序很陌生,我遇到了这个令人困惑的错误。
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.我想要做什么:在 UI 上,我有一个按钮,用于检查 COM3 串行端口是否已打开,如果未打开,则打开它,如果它已打开,则关闭它并在列表框中写一条关于它的消息。 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:我用 Sharpdevelop 的模板创建了应用程序,所以我在几个文件中有代码:
Program.cs:程序.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:主窗体.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) SerialHandle.cs:(这是我的文件,以前在控制台应用程序的Main函数里的东西在这里。DictionaryClass只是一个翻译帮助类,可以轻松地在两种语言之间切换)
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._serialPort.IsOpen,因为它不在上下文中。 SH is created in the Main function, so I don't get why it's not seen.
SH 是在 Main 函数中创建的,所以我不明白为什么没有看到它。 Sorry for the beginner question.
抱歉初学者的问题。
Simply move the initialization statement SerialHandle SH=new SerialHandle();
只需移动初始化语句
SerialHandle SH=new SerialHandle();
into MainForm.cs class进入 MainForm.cs 类
The simplest solution is to pass the SH object instance to the main form:最简单的解决方案是将 SH 对象实例传递给主窗体:
In SerialHandle.cs (use _sh._serialPort.IsOpen)
:在 SerialHandle.cs(使用
_sh._serialPort.IsOpen)
:
private SerialHandle _sh;
public MainForm(SerialHandle sh)
{
_sh = sh;
}
In Program.cs:在 Program.cs 中:
Application.Run(new MainForm(SH));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.