Does not contain a constructor that takes 0 arguments

后端 未结 5 1532
悲哀的现实
悲哀的现实 2020-11-30 03:33

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

public class Products
{
    string          


        
相关标签:
5条回答
  • 2020-11-30 04:16

    As you inherit from Products, you must call a base construct of Products in your own class.

    You didn't write:base(id, name, ....) so C# assumes you call the default parameterless constructor, but it doesn't exist.

    Create a default parameterless constructor for Products.

    0 讨论(0)
  • 2020-11-30 04:21

    Since Products has no constructor that takes 0 arguments, you must create a constructor for FoodProducts that calls the constructor of Products will all the required arguments.

    In C#, this is done like the following:

    public class FoodProducts : Products
    {
    
        public FoodProducts(string id, string name, double price, int soldCount, int stockCount, double tax)   
        : base(id, name, price, soldCount, stockCount, tax)
        {
        }
    
        public void Limit()
        {
            Console.WriteLine("This is an Attribute of a Product");
        }
    }
    

    If you don't want to add this constructor to FoodProducts, you can also create a constructor with no parameter to Products.

    0 讨论(0)
  • 2020-11-30 04:32

    the constructor of the inherited class needs to construct the base class first. since the base class does not have a default constructor (taking 0 arguments) and you are not using the non-default constructor you have now, this won't work. so either A) add a default constructor to your base class, in which case the code of the descending class needs no change; or B) call the non-default constructor of the base class from the constructor of the descending class, in which case the base class needs no change.

    A

    public class Products
    {
        public Products() { }
    }
    
    public class FoodProducts : Products
    {
        public FoodProducts() { }
    }
    

    B

    public class Products
    {
        public class Products(args) { }
    }
    
    public class FoodProducts : Products
    {
        public FoodProducts(args) : base(args) { }
    }
    

    some of this is explained rather OK on msdn here.

    0 讨论(0)
  • 2020-11-30 04:33

    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)
    {
    }
    
    0 讨论(0)
  • 2020-11-30 04:34

    Just add

    public Products()
    {
    
    }
    

    in your products class And you will not get error

    Reason: There exists a default constructor with 0 parameter for every class. So no need to define/write it explicitly (by programmer) BUT when you overload a default constructor with your desired number and type of parameters then it becomes a compulsion to define the default constructor yourself (explicitly) along with your overloaded constructor

    0 讨论(0)
提交回复
热议问题