最好的重载匹配有一些无效的协议 c#

The best overload match has some invalid agruements c#

我正在为四个示例对象创建一个数据库,并且在创建数据库时出现错误。这是构造函数的代码:

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");

您使用了一些错误的参数类型。请尝试使用以下代码:

Licenses[1] = new DriverLicense("Unsupervised at all times", "2/5/2006", 18364, 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"),
  }