没有给定与所需的形式参数-.NET错误相对应的参数

本文关键字:参数 NET 错误 相对 | 更新日期: 2023-09-27 18:21:06

我一直在重构我的一个旧MSSQL连接助手库,我得到了以下错误:

错误CS7036没有给定的与所需的形式参数相对应的参数'ErrorEventArg.ErrorEventArg的'errorMsg'(字符串,string)'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
    }
}

这个错误意味着什么&我该怎么修?我以前从未见过这个错误。。。

没有给定与所需的形式参数-.NET错误相对应的参数

在的构造函数中

public class ErrorEventArg : EventArgs

您必须添加";基本";如下所示:

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

您有一个构造函数,它接受2个参数。你应该写这样的东西:

new ErrorEventArg(errorMsv, lastQuery)

它的代码更少,更容易阅读。

编辑

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

public ErrorEventArg() {}

我得到了同样的错误,但这是由于我没有创建默认构造函数。如果你还没有尝试过,那么创建这样的默认构造函数:

public TestClass()
{
}

我在下面关于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)
    });

当构造函数所需的一个属性不是公共属性时,我收到了这个错误。如果是这种情况,请确保构造函数中的所有参数都指向公共属性:

使用语句命名空间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
  }
}
public ErrorEventArg(string errorMsg, string lastQuery)
    : base (errorMsg, lastQuery)
{
    ErrorMsg = errorMsg;
    LastQuery = lastQuery;
}

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