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

When working with Inheritance in C#, under certain circumstances, you might get an error message similar to this: There is no argument given that corresponds to the required formal parameter.

Don’t worry, the above error can be easily resolved.

Let’s see a full example, by first reproducing the error.

 

Reproducing the Error Message

Consider that you have a console application in C# that uses inheritance, and that you have the below base class:

//base class
public class BaseClass
{
    int id;

    public BaseClass(int i)
    {
        id = i;
    }

    public void baseClassMethod()
    {
        Console.WriteLine("This is the base class");
    }
}

 

Then you try to specify the child class that inherits from the baseClass as below:

//child class
public class childClass : BaseClass
{
    int id2;

    public childClass(int i)
    {
        id2 = i;
    }

    public void childClassMethod()
    {
        Console.WriteLine("This is the child class");
    }
}

 

You then try to compile and you you get the exact error message (for this example): There is no argument given that corresponds to the required formal parameter ‘i’ of ‘BaseClass.BaseClass(int)’

 


Ge the help you need for learning Computer Programming.
Enroll to our Online Course (Lifetime Access)!

Check our online course: Introduction to Computer Programming for Beginners.

This course, will help you get started with C++, C, Python, SQL, Java, C# and learn more about Programming and the Programmer’s Mindset. Moreover, it will help you learn more about the main phases of the Software Development Lifecycle.

Introduction to Computer Programming for Beginners - Online Course
(Lifetime Access, Live Demos, Q&A, Certificate of Completion)

This course, is definitely a must for beginners that are just starting out with computer Programming, but it is also useful for any technical level, since besides the main principles of programming, it also talks about the Programmer’s Mindset, that is the required skill set every great Programmer must have.

Learn More


 

How to Resolve the Issue

To resolve the issue, you just need to add a parameterless constructor in your base class:

//parameterless constructor
public BaseClass()
{
}

 

Here’s the full sample code that works:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritanceBlog
{
    //base class
    public class BaseClass
    {
        int id;

        public BaseClass(int i)
        {
            id = i;
        }

        public BaseClass()
        {
        }

        public void baseClassMethod()
        {
            Console.WriteLine("This is the base class");
        }
    }


    //child class
    public class childClass : BaseClass
    {
        int id2;

        public childClass(int i)
        {
            id2 = i;
        }

        public void childClassMethod()
        {
            Console.WriteLine("This is the child class");
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            childClass c = new childClass(1);
            c.baseClassMethod();
            c.childClassMethod();

            Console.ReadLine();
        }
    }
}

 

Get the Help you Need for Learning Programming – Enroll to our Online Course!

Check our online course “Introduction to Computer Programming for Beginners”.

Via a 6-hour journey, you will learn everything you need to get started with Computer Programming!

The course is being constantly updated with new educational material.

Introduction to Computer Programming for Beginners - Online Course
(Lifetime Access, Live Demos, Q&A, Certificate of Completion)

 

Recommended Online Courses:

 

Read Also:

 

Subscribe to our Newsletter!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub