Base class does not contain a constructor that takes '0' argument

A

Author

I have

class BaseClass
{
public BaseClass(string s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplication1.BaseClass' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.
 
J

Jeroen Mostert

Author said:
I have

class BaseClass
{
public BaseClass(string s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplication1.BaseClass' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.

C# enforces that construction of the base class must be properly completed
before the derived class constructor is executed. Because your derived
constructor doesn't explicitly call a base class constructor, the compiler
automatically inserts a call to the constructor without arguments. But
there's no such constructor, so it fails.

To fix this, you must make a constructor available for the derived class to
call. In this case, initializing the properties does not seem to be
mandatory (since users can pass null for both, and even unset them later) so
there's no problem making a public parameterless constructor available. In
the general case, you could make a protected constructor available
specifically for derived classes to initialize the base state with.
 
A

Ashutosh Bhawasinka

Author said:
I have

class BaseClass
{
public BaseClass(string s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set;}
public string S2 { get; set;}
}

class SubClass: BaseClass
{
public SubClass(int int1, int int2)
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}

VS2008 intellisense underlines the constructor of SubClass and says:

Error 1 'ConsoleApplication1.BaseClass' does not contain a constructor
that takes '0' arguments.

Why? Any hint is highly appreciated.
Let's say you create an object of SubClass like this
SubClass sc = new SubClass(1,2);

Now, when subclass' object is created, it's base class object is also
created internally. So, it's constructor also must be invoked. Now, the
derived class is not explicitly specifying which constructor to use for
the base class, in which case the compiler searches for a constructor
with 0 parameters.

So, to solve this error, you can either add a default constructor to
BaseClass or do something like this

class SubClass : BaseClass
{
public SubClass(int int1, int int2):base("","")
{
this.Int1 = int1;
this.Int2 = int2;
}
public int Int1 { get; set; }
public int Int2 { get; set; }
}
 
N

Nicholas Paldino [.NET/C# MVP]

Author,

The compiler basically assumes this when you write your constructor:

class SubClass : BaseClass
{
public SubClass(int int1, int int2) : base()
{

Since you have defined only one constructor in BaseClass and it takes
arguments, that's where the error comes from. Basically, you need to
explicitly call the constructor on the base class.
 
A

Author

Many thanks to both of you.

Actually, I did notice that if I call the base class's contructor
using :base(blah blah), it error goes away.

So, the conclusion is: either the base class's constructor must be
explicitly called using base() or a parameterless constructor must be
provided for the base class. Correct?
 
J

Jeroen Mostert

Author said:
So, the conclusion is: either the base class's constructor must be
explicitly called using base() or a parameterless constructor must be
provided for the base class. Correct?

Well, *a* base class constructor must be called (you can have any number of
them) and if you don't call one explicitly, there indeed must be a
parameterless one to call implicitly.
 
A

Author

Well, *a* base class constructor must be called (you can have any number of
them) and if you don't call one explicitly, there indeed must be a
parameterless one to call implicitly.

Perfecto! Gotcha. Thx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top