Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class does not contain a constructor that takes 0 arguments [duplicate]

Tags:

c#

.net

I have these 2 classes that are respectively called: Malicious and MaliciousSmall:

Code of Malicious:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;  namespace DataModel.MaliciousCode {     public class Malicious : MaliciousSmall     {     } } 

Code of MaliciousSmall:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.Data; namespace DataModel.MaliciousCode {     public class MaliciousSmall     {         public int Id { get; set; }         public int MaliciousCodeAlertId { get; set; }         public string SourceId { get; set; }         public int MalCodeID { get; set; }         ......................................................         ......................................................         ......................................................          // CONSTRUCTOR:         public MaliciousSmall(DataRow row)         {             Id = int.Parse(row["Id"].ToString());              MaliciousCodeAlertId = (row["MaliciousCodeAlertId"] is DBNull) ? MaliciousCodeAlertId = -1 : MaliciousCodeAlertId = int.Parse(row["MaliciousCodeAlertId"].ToString());             SourceId = (row["SourceId"] is DBNull) ? SourceId = "" : SourceId = row["MaliciousCodeAlertId"].ToString();             MalCodeID = (row["MalCodeID"] is DBNull) ? MalCodeID = -1 : MalCodeID = int.Parse(row["MalCodeID"].ToString());             Title = (row["Title"] is DBNull) ? Title = "" : Title = row["Title"].ToString();          ......................................................         ......................................................         ......................................................         }  } 

My problem is that, after that I have implementet the MaliciousSmall class I obtain the following error on the Malicious constructor:

Error 53 'DataModel.MaliciousCode.MaliciousSmall' does not contain a constructor that takes 0 arguments C:\Develop\EarlyWarning\public\Implementazione\Ver2\DataModel\MaliciousCode\Malicious.cs 9 18 DataModel

What can I do to solve it?

I tried to create an empty constructor that take a DataRow object as paramether, something like it:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.Data;  namespace DataModel.MaliciousCode {     public class Malicious : MaliciousSmall     {          public Malicious(DataRow row)         {          }     } } 

But I still have the same problem. What am I missing? What can I do to solve?

like image 573
AndreaNobili Avatar asked Mar 11 '14 09:03

AndreaNobili


People also ask

Does not contain a constructor that takes arguments?

This error “ClassName does not contain a constructor that takes N arguments” is thrown when you do not write a constructor that accepts those number of arguments you sent while creating the object.

What will be the result if no constructor has been provided?

Explanation: The constructor cannot have a return type. It should create and return new object. Hence it would give compilation error.

What will be the result if no constructor has been provided in C#?

If we do not create constructor the class will automatically call default constructor when an object is created. Example of Default Constructor, using System; namespace DefaultConstractor {


2 Answers

The error is telling you that the Malicious constructor can't find a suitable constructor to chain to in its base class (MaliciousSmall).

The initial problem is that your derived class constructor implicitly contains this constructor:

public Malicious() : base() { } 

Or even when you've added an explicit constructor, it's still trying to chain to a parameterless one:

public Malicious(DataRow row) : base() { } 

... but there is no such parameterless constructor in the base class (MaliciousSmall), hence the error.

I think you want to chain to the constructor taking a row (the one you have declared in MaliciousSmall):

public Malicious(DataRow row) : base(row) { } 

That's assuming you always want a row to be provided to the constructor. If you actually want to allow an instance to be created without specifying a row, then you need to add a parameterless constructor to the base class:

public MaliciousSmall() {     // Set fields/properties to some default values } 

You'd probably then want your derived class to have two constructors:

public Malicious() // Implicitly chain to the parameterless base constructor { }  public Malicious(DataRow row) : base(row) // Chain to parameterized constructor { } 

See my article on constructor chaining for more details.

like image 178
Jon Skeet Avatar answered Oct 20 '22 05:10

Jon Skeet


I tried to create an empty constructor that take a DataRow object as parameter

I think you have the term empty confused. An empty constructor accepts no parameters by definition. You cannot have an empty constructor with parameters.

If you'd like to keep the parameter in the derived class's constructor too, you need to use the base keyword to specify the base constructor.

public Malicious(DataRow row)     : base(row) { } 
like image 37
Connell Avatar answered Oct 20 '22 07:10

Connell