不包含接受1个参数“”的构造函数;4〃;
本文关键字:构造函数 包含接 1个 参数 | 更新日期: 2023-09-27 18:28:19
我发现有很多人问这个错误,但我可以找到一个合适的答案并对此进行解释。
类别:Update
public class Log
{
public int id { get; set; }
public string description { get; set; }
public string type { get; set; }
public string action { get; set; }
public DateTime date { get; set; }
public string fullname { set; get; }
public Log() {
}
public Log(LogDs.LogFileRow row)
{
this.id = row.id;
this.type = row.type;
this.action = row.action.ToString();
this.description = row.description.ToString();
this.date = row.date;
this.fullname = row.fullname;
}
}
Bc类别:
public class LogBC
{
LogDalc dalc = new LogDalc();
public List<Log> List(DateTime dateStart, DateTime dateEnd)
{
List<Log> listLog = new List<Log>();
LogDs.LogFileDataTable dt = dalc.Sel(dateStart, dateEnd);
for (int i = 0; i < dt.Count; i++)
{
Log exp = new Log(dt[i]);
listLog.Add(exp);
}
return listLog;
}
Dalc等级:
public class LogDalc: BaseDalc
{
public LogDs.LogFileDataTable Sel(DateTime dateStart, DateTime dateEnd)
{
LogDs ds = new LogDs();
using (SqlConnection con = new SqlConnection(Global.Config.ConnStr))
{
SqlCommand cmd = new SqlCommand("spp_txn_expenses_sel", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@sdate", dateStart == DateTime.MinValue ? dbNull : dateStart));
cmd.Parameters.Add(new SqlParameter("@edate", dateEnd == DateTime.MinValue ? dbNull : dateEnd));
FillDataSet(cmd, ds, new string[] { ds.LogFile.TableName });
}
return ds.LogFile;
}
}
错误显示在Bc类中,而准确地显示在CCD_ 2中。有人能解释为什么会发生这种情况以及如何解决吗?
您的Log
类不知道如何处理您传递的参数。您需要定义一个接受这种参数的构造函数:
public Log(LogDs.LogFileDataTable dt)
{
// initialize your settings from the DataTable here
}
Log
类上没有接受单个参数的构造函数。
提供参数化构造函数:
public class Log
{
public int id { get; set; }
public string description { get; set; }
public string type { get; set; }
public string action { get; set; }
public DateTime date { get; set; }
public string fullname { set; get; }
public Log(object myValue) {
// your logic for myValue assignment to some property
}
}
或者使用属性的内联分配
Log exp = new Log() { Description = dt[i].ToString() };