Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

like image 678
omic96 Avatar asked Sep 18 '20 13:09

omic96


People also ask

What is error CS7036?

Error CS7036 There is no argument given that corresponds to the required formal parameter (Xamarin Forms)

What is an argument in C#?

A parameter is the variable listed inside the parentheses in the function definition. An argument is the actual value that is sent to the function when it is called. Below are examples of each. // a and b are parameters. public int add(int a, int b)

How can use base constructor in C#?

base (C# Reference) The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

What is a default constructor C#?

A constructor with no parameters is called a default constructor. A default constructor has every instance of the class to be initialized to the same values. The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class. Example : C#


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.)

like image 113
David Avatar answered Sep 29 '22 01:09

David