没有给定的参数对应于所需的形式参数 - .NET 错误
There is no argument given that corresponds to the required formal parameter - .NET Error
我一直在重构我的一个旧 MSSQL 连接帮助程序库,但出现以下错误:
Error CS7036 There is no
argument given that corresponds to the required formal parameter
'errorMsg' of 'ErrorEventArg.ErrorEventArg(string,
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
}
}
这个错误是什么意思,我该如何解决?我以前没见过这个错误...
您有一个带有 2 个参数的构造函数。你应该这样写:
new ErrorEventArg(errorMsv, lastQuery)
它的代码更少,更易于阅读。
编辑
或者,为了您的工作方式,您可以尝试为 ErrorEventArg 编写一个没有参数的默认构造函数,如下所示:
public ErrorEventArg() {}
当构造函数所需的属性之一不是 public 时,我收到此错误。如果是这种情况,请确保构造函数中的所有参数都转到 public 的属性:
使用语句
命名空间 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 class ErrorEventArg : EventArgs
您必须按如下方式添加“基础”:
public ErrorEventArg(string errorMsg, string lastQuery) : base (string errorMsg, string lastQuery)
{
ErrorMsg = errorMsg;
LastQuery = lastQuery;
}
我遇到了同样的错误,但这是因为我没有创建默认构造函数。如果您还没有尝试过,请像这样创建默认构造函数:
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)
});
public ErrorEventArg(string errorMsg, string lastQuery)
: base (errorMsg, lastQuery)
{
ErrorMsg = errorMsg;
LastQuery = lastQuery;
}
要调用基础构造函数,sub class 和 base class 的参数必须相同。
我一直在重构我的一个旧 MSSQL 连接帮助程序库,但出现以下错误:
Error CS7036 There is no argument given that corresponds to the required formal parameter 'errorMsg' of 'ErrorEventArg.ErrorEventArg(string, 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
}
}
这个错误是什么意思,我该如何解决?我以前没见过这个错误...
您有一个带有 2 个参数的构造函数。你应该这样写:
new ErrorEventArg(errorMsv, lastQuery)
它的代码更少,更易于阅读。
编辑
或者,为了您的工作方式,您可以尝试为 ErrorEventArg 编写一个没有参数的默认构造函数,如下所示:
public ErrorEventArg() {}
当构造函数所需的属性之一不是 public 时,我收到此错误。如果是这种情况,请确保构造函数中的所有参数都转到 public 的属性:
使用语句 命名空间 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 class ErrorEventArg : EventArgs
您必须按如下方式添加“基础”:
public ErrorEventArg(string errorMsg, string lastQuery) : base (string errorMsg, string lastQuery)
{
ErrorMsg = errorMsg;
LastQuery = lastQuery;
}
我遇到了同样的错误,但这是因为我没有创建默认构造函数。如果您还没有尝试过,请像这样创建默认构造函数:
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)
});
public ErrorEventArg(string errorMsg, string lastQuery)
: base (errorMsg, lastQuery)
{
ErrorMsg = errorMsg;
LastQuery = lastQuery;
}
要调用基础构造函数,sub class 和 base class 的参数必须相同。