Kompilatorfel CS1729

"type" innehåller inte en konstruktor som tar talargument.

Det här felet uppstår när du antingen direkt eller indirekt anropar konstruktorn för en klass, men kompilatorn inte kan hitta några konstruktorer med samma antal parametrar. I följande exempel test har klassen inga konstruktorer som tar några argument. Den har därför bara en parameterlös konstruktor som tar noll argument. Eftersom den härledda klassen på den andra raden där felet genereras inte deklarerar några egna konstruktorer, tillhandahåller kompilatorn en parameterlös konstruktor. Konstruktorn anropar en parameterlös konstruktor i basklassen. Eftersom basklassen inte har någon sådan konstruktor genereras CS1729.

Så här åtgärdar du det här felet

  1. Justera antalet parametrar i anropet till konstruktorn.

  2. Ändra klassen för att tillhandahålla en konstruktor med de parametrar som du måste anropa.

  3. Ange en parameterlös konstruktor i basklassen.

Exempel

I följande exempel genereras CS1729:

// cs1729.cs  
class Test  
{  
    static int Main()  
    {  
        // Class Test has only a parameterless constructor, which takes no arguments.  
        Test test1 = new Test(2); // CS1729  
        // The following line resolves the error.  
        Test test2 = new Test();  
  
        // Class Parent has only one constructor, which takes two int parameters.  
        Parent exampleParent1 = new Parent(10); // CS1729  
        // The following line resolves the error.  
        Parent exampleParent2 = new Parent(10, 1);  
  
        return 1;  
    }  
}  
  
public class Parent  
{  
    // The only constructor for this class has two parameters.  
    public Parent(int i, int j) { }  
}  
  
// The following declaration causes a compiler error because class Parent  
// does not have a constructor that takes no arguments. The declaration of  
// class Child2 shows how to resolve this error.  
public class Child : Parent { } // CS1729  
  
public class Child2 : Parent  
{  
    // The constructor for Child2 has only one parameter. To access the
    // constructor in Parent, and prevent this compiler error, you must provide
    // a value for the second parameter of Parent. The following example provides 0.  
    public Child2(int k)  
        : base(k, 0)  
    {  
        // Add the body of the constructor here.  
    }  
}  

Se även