Nikhil V
Nikhil V

Reputation: 33

How to use base constructor data to another constructor in the same class?

while trying to call base constructor it throw error like "object doesn't contain constructor that take one argument"

    public string FirstName { get;private set; }
    public string LastName { get;private set; }

    public Employee(string firstName)
    {
        FirstName = firstName;
    }

    public Employee(string firstName,string lastName):base(firstName)//error
    {
        LastName = lastName;
    }

    public string SayHello()
    {
        return FirstName + " " + LastName;
    }

thanks

Upvotes: 3

Views: 46

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

You'd probably want to call current class constructor, not a base class constructor:

public Employee(string firstName, string lastName): this(firstName) // this, not base
{
    LastName = lastName;
}

Upvotes: 8

Related Questions