我一直在重构我的旧 MSSQL 连接帮助程序库之一,但出现以下错误:
错误CS7036 没有 给出的参数对应于所需的形式参数 '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
}
}
此错误意味着什么以及如何修复它?我以前没有见过这个错误...
在
的构造函数中public class ErrorEventArg : EventArgs
您必须添加“base”,如下所示:
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
}
}
在尝试构建解决方案时,我也遇到了此错误消息,其中另一个开发人员从构造函数中删除了参数(没有继承),但旧程序集位于 GAC 中。
将其从 GAC 中删除后,它就能够使用新代码成功构建。
public ErrorEventArg(string errorMsg, string lastQuery)
: base (errorMsg, lastQuery)
{
ErrorMsg = errorMsg;
LastQuery = lastQuery;
}
调用基类构造函数时,子类和基类的参数必须相同。