最佳重载匹配具有一些无效的集合 c#
本文关键字:无效 集合 具有一 重载 最佳 | 更新日期: 2023-09-27 17:55:11
我正在为四个示例对象创建一个数据库,在创建数据库时,我收到错误。 下面是构造函数的代码:
public DriverLicense(string condition, string dateofissue, int number, int type)
{
Condition = condition;
DateOfIssue = dateofissue;
Number = number;
Type = type;
}
以防万一,数据库的代码:
DriverLicense[] Licenses = new DriverLicense[4];
Licenses[0] = new DriverLicense("Supervised at all times", "3/6/2013", "176325", "2");
Licenses[1] = new DriverLicense("Unsupervised at all times", "2/5/2006", "18364", "3");
Licenses[2] = new DriverLicense("Supervised at all times", "6/1/2011", "472957", "2");
Licenses[3] = new DriverLicense("Unsupervised at all times", "8/4/2009", "648217", "3");
构造函数需要两个String
和两个int
public DriverLicense(string condition, string dateofissue, int number, int type) {
...
}
但是您发送了四个string
:
// "176325" is a String as well as "2"
new DriverLicense("Supervised at all times", "3/6/2013", "176325", "2");
可能的解决方案是更改实例创建
DriverLicense[] Licenses = new DriverLicense[] {
// Note 176325 and 2 are int, not string
new DriverLicense("Supervised at all times", "3/6/2013", 176325, 2),
new DriverLicense("Unsupervised at all times", "2/5/2006", 18364, 3),
new DriverLicense("Supervised at all times", "6/1/2011", 472957, 2),
new DriverLicense("Unsupervised at all times", "8/4/2009", 648217, 3),
}
或实现重载构造函数:
public DriverLicense(string condition, string dateofissue, String number, String type)
: this(condition, dateofissue, int.Parse(number), int.Parse(type)) {
}
所以你可以很容易地把
DriverLicense[] Licenses = new DriverLicense[] {
// the overloaded constructor accepts strings
new DriverLicense("Supervised at all times", "3/6/2013", "176325", "2"),
new DriverLicense("Unsupervised at all times", "2/5/2006", "18364", "3"),
new DriverLicense("Supervised at all times", "6/1/2011", "472957", "2"),
new DriverLicense("Unsupervised at all times", "8/4/2009", "648217", "3"),
}
您使用了一些错误的参数类型。请尝试使用以下代码:
Licenses[1] = new DriverLicense("Unsupervised at all times", "2/5/2006", 18364, 3);