Error CS7036: There is no argument given that corresponds to the required formal parameter

Viewed 25026

I'm trying to figure out why I'm getting this error, I'm not sure what I should do, I've tried to delete parameters and then it works, but I need it to work with parameters. The class "Manager" that inherits from "Employee" gives an error.

using System;

class MainClass
{
    class Employee
    {
        public string name;
        public string jobTitle;
        public double salary;

        public Employee(string empName, string empJobTitle, double empSalary)
        {
            

        }

        public double calculateYearlySalary(double salary)
        {
            double yearlySalary = salary * 12;

            return yearlySalary;
        }

    }

    class Manager : Employee
    {
        public int bonus;

        public Manager(string empName, string empJobTitle, double empSalary, int empBonus)
        {

        }

    }

    public static void Main()
    {
        Employee emp1 = new Employee("John", "Junior Software Engineer", 32.45);
        Manager emp2 = new Manager("David", "Senior Software Engineer", 62.20, 10);

        Console.WriteLine(emp1.calculateYearlySalary(100.00));
    }

}

The error exactly:

"/Users/Oliver/Projects/printstars/printstars/Program.cs(16,16): Error CS7036: There is no argument given that corresponds to the required formal parameter 'empName' of 'MainClass.Employee.Employee(string, string, double)' (CS7036) (printstars)"

1 Answers

Employee has no parameterless constructor, so anything which inherits from it needs to specify the values to send to its constructor. You can do this with constructor chaining:

public Manager(string empName, string empJobTitle, double empSalary, int empBonus)
    : base(empName, empJobTitle, empSalary)
    {

    }

The call to base tells the Manager constructor that it should pass those three parameters along to the Employee constructor when creating an instance of itself.

(Side note: You probably also meant to set your class fields within your constructors. Otherwise those values are just going to be ignored.)

Related