The best overload match has some invalid agruements c#

72 Views Asked by At

I am making a database for four sample objects, and when create the database, i get the error. here is the code for the constructor:

public DriverLicense(string condition, string dateofissue, int number, int type)
{
    Condition = condition;
    DateOfIssue = dateofissue;
    Number = number;
    Type = type;

}

and just in case, the code for the database:

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

There are 2 best solutions below

0
On BEST ANSWER

Your constructor expects two Strings and two ints

  public DriverLicense(string condition, string dateofissue, int number, int type) {
    ...
  }

but you send four strings:

  // "176325" is a String as well as "2"
  new DriverLicense("Supervised at all times", "3/6/2013", "176325", "2");

possible solution is either to change instances creation

  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),
  }

or implement an overload constructor:

  public DriverLicense(string condition, string dateofissue, String number, String type)
    : this(condition, dateofissue, int.Parse(number), int.Parse(type)) {
  }

so you can easily put

  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"),
  }
0
On

You are using some wrong argument types. Please try using following code:

Licenses[1] = new DriverLicense("Unsupervised at all times", "2/5/2006", 18364, 3);