没有给出对应于所需形式参数的参数 - .NET Error

时间:2015-11-09 10:32:38

标签: c# .net .net-4.5

我一直在重新考虑我的一个旧的MSSQL Connection助手库,我收到了以下错误:

  

严重级代码说明项目文件行错误CS7036没有   给出的参数对应于所需的形式参数   'errorEsg'的'ErrorEventArg.ErrorEventArg(字符串,   字符串)'MSSQLTest C:\ Users \ Administrator \ Desktop \ MSSQLTest \ MSSQLTest \ MSSQLConnection.cs 61

到目前为止,这是我的代码:

MSSQLConnection.cs

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;

namespace MSSQLTest
{
    public class ErrorEventArg : EventArgs
    {
        public string ErrorMsg { get; set; }
        public string LastQuery { get; set; }

        public ErrorEventArg(string errorMsg, string lastQuery)
        {
            ErrorMsg = errorMsg;
            LastQuery = lastQuery;
        }
    }

    public class MSSQLConnection
    {
        /// <summary>
        /// Private class objects.
        /// </summary>
        private SqlConnection sqlConnection;
        private int sqlCommandTimeout;
        private string lastQuery = string.Empty;

        /// <summary>
        /// Public event related objects & handler.
        /// </summary>
        public event ErrorHandler OnError;
        public delegate void ErrorHandler(MSSQLConnection sender, ErrorEventArg e);

        /// <summary>
        /// Class constructor.
        /// </summary>
        /// <param name="sqlConnection"></param>
        /// <param name="sqlCommandTimeout"></param>
        public MSSQLConnection(SqlConnection sqlConnection, Int32 sqlCommandTimeout = 120)
        {
            if (null == sqlConnection)
                throw new Exception("Invalid MSSQL Database Conection Handle");

            if (sqlConnection.State != System.Data.ConnectionState.Open)
                throw new Exception("MSSQL Database Connection Is Not Open");

            this.sqlConnection = sqlConnection;
            this.sqlCommandTimeout = sqlCommandTimeout;
        }

        /// <summary>
        /// Helper method to emit a database error to event subscribers.
        /// </summary>
        /// <param name="errorMsg"></param>
        internal void EmitError(String errorMsg)
        {
            var errorDelegate = OnError;
            if (errorDelegate != null)
            {
                errorDelegate(this, new ErrorEventArg() // Line #61
                {
                    ErrorMsg = errorMsg,
                    LastQuery = lastQuery
                });
            }
        }

        /// rest of the code snipped
    }
}

此错误意味着什么?我如何解决它?我之前没见过这个错误......

6 个答案:

答案 0 :(得分:5)

你有一个带2个参数的构造函数。你应该写一些类似的东西:

new ErrorEventArg(errorMsv, lastQuery)

代码少,易于阅读。

修改

或者,为了您的工作方式,您可以尝试为ErrorEventArg编写一个没有参数的默认构造函数,如下所示:

public ErrorEventArg() {}

答案 1 :(得分:2)

的构造函数中
 public class ErrorEventArg : EventArgs

您必须添加“ base”,如下所示:

    public ErrorEventArg(string errorMsg, string lastQuery) : base (string errorMsg, string lastQuery)
    {
        ErrorMsg = errorMsg;
        LastQuery = lastQuery;
    }

为我解决了

答案 2 :(得分:1)

我在以下有关DailyReport的Linq语句中收到了相同的错误。问题在于DailyReport没有默认构造函数。显然,它会在填充属性之前实例化对象。

var sums = reports
    .GroupBy(r => r.CountryRegion)
    .Select(cr => new DailyReport
    {
        CountryRegion = cr.Key,
        ProvinceState = "All",
        RecordDate = cr.First().RecordDate,
        Confirmed = cr.Sum(c => c.Confirmed),
        Recovered = cr.Sum(c => c.Recovered),
        Deaths = cr.Sum(c => c.Deaths)
    });

答案 3 :(得分:0)

当构造函数所需的属性之一不公开时,出现此错误。在这种情况下,请确保构造函数中的所有参数都进入公共属性:

使用语句 命名空间someNamespace

public class ExampleClass {

  //Properties - one is not visible to the class calling the constructor
  public string Property1 { get; set; }
  string Property2 { get; set; }

   //Constructor
   public ExampleClass(string property1, string property2)
  {
     this.Property1 = property1;
     this.Property2 = property2;  //this caused that error for me
  }
}

答案 4 :(得分:0)

我遇到了同样的错误,但这是由于我没有创建默认构造函数。如果您还没有尝试过,请创建默认的构造函数,如下所示:

公共TestClass() {

}

答案 5 :(得分:-1)

public ErrorEventArg(string errorMsg, string lastQuery)
    : base (errorMsg, lastQuery)
{
    ErrorMsg = errorMsg;
    LastQuery = lastQuery;
}

要调用基类构造函数,子类和基类的参数必须相同。