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.
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.
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.
Recommended Online Courses:
- AI Essentials: A Beginner’s Guide to Artificial Intelligence
- SQL Server 2022: What’s New – New and Enhanced Features
- Working with Python on Windows and SQL Server Databases
- Introduction to Azure Database for MySQL
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- Data Management for Beginners – Main Principles
- A Guide on How to Start and Monetize a Successful Blog
Read Also:
- How to Set Filters for OpenFileDialog and SaveFileDialog in C#
- How to Connect to SQL Server from Visual C++
- What is Abstraction in Object Oriented Programming?
- How to Establish a Simple Connection from a C# Program to SQL Server
- The timeout period elapsed prior to obtaining a connection from the pool
- Closing a C# Application (including hidden forms)
- Changing the startup form in a C# project
- Using the C# SqlParameter Object for Writing More Secure Code
- Cannot implicitly convert type ‘string’ to ‘System.Windows.Forms.DataGridViewTextBoxColumn
- Using the C# SqlParameter Object for Writing More Secure Code
- How to Build a Simple Image Viewer with .NET WinForms and C# in Visual Studio
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.