Skip to main content
 首页 » 编程设计

c# 不能从静态函数中引用

2024年02月24日30lovecherry

这里是 c# 新手。

似乎这个用户为我的问题提供了一个很好的解决方案:

serialport error

但我不知道如何编写他建议的代码。你能帮忙吗?

请您参考如下方法:

这将编译正常。当然,您必须提供 thePort 真实设置。

namespace csWinFormsTest 
{ 
    public partial class Form1 : Form 
    { 
        static System.IO.Ports.SerialPort thePort; 
        public Form1() 
        { 
            InitializeComponent(); 
            thePort = new System.IO.Ports.SerialPort("COM1"); 
        } 
 
        static void fcn() 
        { 
            MessageBox.Show(thePort.PortName); 
        } 
    } 
}