简体   繁体   English

不包含接受1个参数的构造方法

[英]Does Not Contain A Constructor That Takes 1 Arguments

I am a bit new to C# and I recently ran into an issue receiving an "Invalid Operation Exception" when my serial connection is lost during a serial communication operation. 我对C#有点陌生,最近在串行通信操作期间丢失串行连接时遇到了“接收无效操作异常”的问题。 I am trying to catch the error via private void port_ErrorReceived (see below) but I keep receiving an error stating "does not contain a constructor that takes 1 arguments". 我试图通过private void port_ErrorReceived捕获错误(请参见下文),但我不断收到错误,指出“不包含采用1个参数的构造函数”。

    private void port_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
    {
        bool error = false;

        // Check if the comport is closed
        if (!comport.IsOpen)
        { 
                try
                {
                    // Try to open the port
                    comport.Open();
                }
                catch (UnauthorizedAccessException) { error = true; }
                catch (IOException) { error = true; }
                catch (ArgumentException) { error = true; }
                catch (InvalidOperationException) { error = true; }

                if (error) MessageBox.Show(this, "No serial port identified. Please check your connection.", "Serial Connection Lost", MessageBoxButtons.OK, MessageBoxIcon.Stop);
         }     
    }

Here is where I call my new event handler: 这是我称之为新事件处理程序的地方:

     comport.ErrorReceived += new SerialErrorReceivedEventArgs(port_ErrorReceived);

I've seen some similar posts on StackOverflow but I wasn't quite sure what applied to this scenario. 我在StackOverflow上看到过一些类似的帖子,但是我不太确定什么适用于这种情况。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

I think you mean simply: 我想你的意思很简单:

comport.ErrorReceived += port_ErrorReceived;

or more verbosely: 或更详细地说:

comport.ErrorReceived += new SerialErrorReceivedEventHandler(port_ErrorReceived);

(but they are identical; there's no reason not to use the first version) (但它们是相同的;没有理由不使用第一个版本)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM