I am trying to build a class hierarchy (the Main constructor is called from Main, which, depending on some parameters, calls one of three subclasses.) The compiler generates an error "***" does not contain a constructor that takes arguments "and" member names cannot match the types in which they are contained. " How can I fix this?

Call base class

var Price=new Edition(EditionType, PagesNumber, PrintType); 

Base class itself

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PriceCalculator { public class Edition { private readonly int EditionType; private readonly int PagesNumber; private readonly int PrintType; public Edition(int EditionType, int PagesNumber, int PrintType) { this.EditionType = EditionType; this.PagesNumber = PagesNumber; this.PrintType = PrintType; int ApproxPrice = PagesNumber * PrintType; Calculate(ApproxPrice); } public int Calculate (int Price) { if (EditionType == 1) { int value = Convert.ToInt32(new Journal(Price)); return value; } else { return 0; } } } } 

Journal class

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PriceCalculator { public class Journal : Edition { private readonly int ApproxPrice; public Journal(int ApproxPrice) { this.ApproxPrice = ApproxPrice; ControlPrice(ApproxPrice); } public int ControlPrice(int Price) { Console.WriteLine("Test"); var mult = Convert.ToInt32(Console.Read()); return Convert.ToInt32(ApproxPrice * mult); } } } 
  • one
    What it is? public int Journal(int ApproxPrice) are you trying to return a value in the constructor? - LLENN
  • one
    The constructor returns a class object, in this case, an attempt is made to call a method as a constructor. - Grundy

1 answer 1

The designer cannot return something.

 public /*int*/ Edition(int EditionType, int PagesNumber, int PrintType) { this.EditionType = EditionType; this.PagesNumber = PagesNumber; this.PrintType = PrintType; ApproxPrice = PagesNumber * PrintType; } 

I changed the code, but one error remained in the Journal class "There is no argument corresponding to the required formal parameter" EditionType "from" Edition.Edition (int, int, int) "

The parent class Edition no constructor with no parameters, which the compiler tries to insert into its code. So he complains about the absence of the first EditionType parameter in this call.

  public Journal(int ApproxPrice) : base(?, ?, ?) { this.ApproxPrice = ApproxPrice; ControlPrice(ApproxPrice); } 
  • 2
    another public int Journal(int ApproxPrice) . - LLENN
  • one
    @LLENN Naturally. - Igor
  • one
    @Alexey You are trying to access PrintType using the Edition type. And you need to use an instance of the class - an object of type Edition . - Igor
  • one
    @Alexey Show your code with Edition.PrintType . - Igor
  • one
    @Alexey Reread my comment above. Do not hurry. Think about it. - Igor