Rasmus Hye
Rasmus Hye

Reputation: 33

Problems with converting the DateTime input

I'm new to OOP, and are writing a Tennis Tournament program, where the players have the following data (I made the Person class a superclass, because I later need to connect a referee class with some of the same instance variables). When I try to create a new player in my SVM method, I cannot create, because I get an error with the birthdate input. When I try to input 1981/09/26 it says: "Argument 4: cannot convert from 'int' to System.DateTime'.

What can I do to fix this? The other inputs like string and the GenderType works fine?

public class Person
{
    public enum GenderType     { Male, Female}
    private string FName       { get; set; }
    private string MName       { get; set; }
    private string LName       { get; set; }
    private DateTime Birthdate { get; set; }
    private string Nationality { get; set; }
    private GenderType Gender  { get; set; }

    public Person(string fn, string mn, string ln, DateTime bd, string nation, GenderType g)
    {
        FName = fn;
        MName = mn;
        LName = ln;
        Birthdate = bd;
        Nationality = nation;
        Gender = g;
    }

    public override string ToString(){ return "Firstname: " + FName + " Middlename: " + MName + " Lastname: " + LName + " Birthdate: " + Birthdate + " Nationality " + Nationality + " Gender: " + Gender;}}

public class Player : Person {

public Player(string fn, string mn, string ln, DateTime bd, string nation, GenderType g) : base(fn, mn, ln, bd, nation, g)
    {

    }

    static void Main(string[] args)
    {
        var p1 = new Player("Serena", "J.", "Williams", 1981 / 09 / 26, "USA", GenderType.Female);
    }


}

Upvotes: 0

Views: 97

Answers (3)

You can set date time like this :

static void Main(string[] args)
{
    var p1 = new Player("Serena", "J.", "Williams", new DateTime (1981, 09, 26, 0, 0, 0), "USA", GenderType.Female);
}

Upvotes: 0

Sergey
Sergey

Reputation: 11

var p1 = new Player("Serena", "J.", "Williams", new Datetime(1981,09,26), "USA", GenderType.Female);

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

Because you are not passing a DateTime type instance, instead you are passing literal directly which compiler is considering as of type int but it is clear from your code you have Birthdate property in your class of type DateTime.

You need to create a DateTime type object :

new Player("Serena", "J.", "Williams",new DateTime(1981,09,26),"USA", GenderType.Female);

The first parameter is year, 2nd is month and 3rd is day of month in the DateTime constructor.

Following is the constructor being used for it.

Upvotes: 7

Related Questions