Author wrote:
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; }
}