Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does not contain a constructor that takes 0 arguments

I get an error stating "Products does not contain a constructor that takes 0 arguments" from the following code:

public class Products {     string id;     string name;     double price;     int soldCount;     int stockCount;      public Products(string id, string name, double price,                        int soldCount, int stockCount, double tax)     {         this.id = id;         this.name = name;         this.price = price;         this.soldCount = soldCount;         this.stockCount = stockCount;     } }  //I have got some get and set values for the code above  //but it would have been too long to put in here  public class FoodProducts : Products {     public FoodProduct()     {         Console.WriteLine("This is food product");     }      public void Limit()     {         Console.WriteLine("This is an Attribute of a Product");     } } 
like image 513
user1618490 Avatar asked Aug 27 '12 08:08

user1618490


People also ask

Which constructor does not take any argument?

No-argument constructor: A constructor that has no parameter is known as the default constructor. If we don't define a constructor in a class, then the compiler creates a default constructor(with no arguments) for the class.

What is zero parameterized constructor?

A zero parameter (or "no-arg") constructor is one that requires no arguments. A class can have multiple constructors, each with different arguments. Constructors without arguments are particularly helpful for tools that use reflection to populate an object's properties.

Can constructor take only one argument?

In C++, if a class has a constructor which can be called with a single argument, then this constructor becomes a conversion constructor because such a constructor allows automatic conversion to the class being constructed.

Can constructor take any number of arguments?

A constructor having a specific number of parameters(arguments) is called a parameterized constructor. The parameterized constructor is used to provide different values to the objects, you can also provide the same values.


1 Answers

Several rules about C# come into play here:

  1. Each class must have a constructor (In order to be, well constructed)

  2. If you do not provide a constructor, a constructor will be provided for you, free of change, automatically by the compiler.

    This means that the class

    class Demo{} 

    upon compilation is provided with an empty constructor, becoming

    class Demo{    public Demo(){} } 

    and I can do

    Demo instance = new Demo(); 
  3. If you do provide a constructor (any constructor with any signature), the empty constructor will not be generated

    class Demo{    public Demo(int parameter){} }  Demo instance = new Demo(); //this code now fails Demo instance = new Demo(3); //this code now succeeds 

    This can seem a bit counter-intuitive, because adding code seems to break existing unrelated code, but it's a design decision of the C# team, and we have to live with it.

  4. When you call a constructor of a derived class, if you do not specify a base class constructor to be called, the compiler calls the empty base class constructor, so

    class Derived:Base {    public Derived(){} } 

    becomes

    class Derived:Base {    public Derived() : base() {} } 

So, in order to construct your derived class, you must have a parameterless constructor on the base class. Seeing how you added a constructor to the Products, and the compiler did not generate the default constructor, you need to explicitly add it in your code, like:

public Products() { } 

or explicitly call it from the derived constructor

public FoodProduct()        : base(string.Empty, string.Empty, 0, 0, 0, 0) { } 
like image 190
SWeko Avatar answered Sep 28 '22 11:09

SWeko