Home > database >  There is no argument given that corresponding to the required parameter error
There is no argument given that corresponding to the required parameter error

Time:09-13

I am creating a code which ask the users input for its first name, last name, hours, and wages. Then the hours and wages are then calculated and will be shown when called. Now I got this error "There is no argument given that corresponds to the required formal parameter 'input 1' of 'Human.Human(string,string)" shown on what should I change or what constructors do I need to fix?

    using EncapsulationConcept;
    using System;

    namespace EncapsulationConcept
    {
    class Human
    {
    private string value1;
    private string value2;

    public Human(string input1, string input2)
    {
        value1 = input1;
        value2 = input2;
    }
    public void DisplayInfo()
    {
        Console.WriteLine("\nfirst name is: {0}", value1);
        Console.WriteLine("last name is: {0}", value2);
    }
}
class Worker : Human
{
    private int Hours;
    private int wages = 50;
    private int result;
    public  int HourlyWage(int hour)
    {
        Hours = hour;
        result = hour * wages;
        return hour;
    }
    public void DisplayInfo()
    {
        Console.WriteLine("\nNo. of hours in {0}", result);
    }
}
   }

class Program
{
    static void Main(string[] args)
    {
        string input1, input2;
        int hour;

        Console.Write("First name : ");
        input1 = Convert.ToString(Console.ReadLine());
        Console.Write("Last name: ");
        input2 =  Convert.ToString(Console.ReadLine());
        Console.Write("Enter hours : ");
        hour = Convert.ToInt32(Console.ReadLine());
       
        Human human = new Human(input1, input2);
        human.DisplayInfo();
    }
}

CodePudding user response:

The error is telling you that the Worker class can't be created because it has no way to create the underlying Human type. Human has a constructor with parameters:

public Human(string input1, string input2)

But Worker does not. So if you were to do new Worker() then the code has no way to supply those parameters to Human.

You'd either need to allow a parameterless Human:

public Human() { }

Or add a Worker constructor which provides parameters to Human:

public Worker(string input1, string input2) : base(input1, input2) { }

However you do it, any time you create an instance of an inherited type it needs to be able to create an instance of the underlying base type.

CodePudding user response:

If the constructor of the mother class contains two parameters, the inherited classes must show these two parameters in their constructor. Change as follows:

If value1 and value2 are private, your children cannot acces them.

 class Human
 {
      internal string value1;
      internal string value2;
      ...
 }

Change the children constructor

 class Worker : Human
 {
      public Worker(string input1, string input2) : base(input1, input2)
      {
           value1 = input1;
           value2 = input2;
      }
      ...
 }
  •  Tags:  
  • c#
  • Related