Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve error 'there is no argument given that corresponds to required formal parameter'?

Tags:

c#

oop

I have following code where I'm getting error while compiling in C# visual Studio 2015.

class Oval:Shape
{
    private double major_axis, minor_axis;
    public Oval(double Major_Axis, double Minor_Axis)
    {
      major_axis = Major_Axis;
      minor_axis = Minor_Axis;
    } //Constructor
}
class Circle:Oval
{
    private double radius;
    public Circle(double Circle_Radius) // Getting Error on this line
    {
      radius = Circle_Radius;  
    } //constructor
}
like image 247
Dhiraj Sardal Avatar asked Aug 04 '16 04:08

Dhiraj Sardal


People also ask

What is error CS7036?

Error CS7036 There is no argument given that corresponds to the required formal parameter (Xamarin Forms) - Microsoft Q&A.

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.


1 Answers

Fixing your bug:

The error occurs due to the lack of a parameterless constructor (or your lack of using the base() method in your constructor (just like user3185569 had said)

Fixing your code:

It clearly seems you are lacking some basics in .NET so I've decided to give a re-writing to your code with the following things in mind:

a. Conventions

There are some rules about common conventions that should apply to your code.

Members usually begin with either m or _ and then the memberName (camel casing).

Properties are usually written regularly as PropertyName and same applies to methods.

Parameters and variables are simply camel cased like parameterName

b. Access Modifiers

I don't know the use of your Oval and circle but I assume you'd want to access them outside of Oval and Circle.

I think it would be the best to reference you to here to read some more about the topic: https://msdn.microsoft.com/en-us/library/ms173121.aspx

I've re-written your code to include all those tips (and also fix your issue)

public class Oval:Shape
{       
    //Constructor
    public Oval(double majorAxis, double minorAxis)
    {
        MajorAxis=majorAxis;
        MinorAxis=minorAxis;
    } 
    
    protected double MajorAxis{ get; set; }     
    protected double MinorAxis{ get; set; }     
}    

public class Circle:Oval
{       
    //Constructor
    public Circle(double radius): base(radius,radius)
    {
        radius = Circle_Radius;  
    }
    
    public double Radius
    {
        get
        {
            return MajorAxis;
        }
        set
        {
            MajorAxis = value;
            MinorAxis = value;
        }       
    }
}
like image 101
shirbr510 Avatar answered Oct 15 '22 00:10

shirbr510