Your base class constructor is this:
public Customer(int _Cid,int _Bal,String _Cname)
Your derived class constructor is this:
public Stat(bool _Status)
In C# when you instantiate a derived class, the base class must be called. Where the base class only has a parameterless constructor, this is done implicitly before the derived class constructor body is executed. If the base class does not have a parameterless constructor, you must explicitly call it using base
.
Take this example:
public enum MyEnum
{
Person,
Animal,
Derived
}
public class Base
{
public Base(MyEnum classType)
{
}
}
public class Person : Base
{
}
There’s two ways you can do this: accept the argument to Person
and pass it to the base constructor:
public class Person : Base
{
public Person(MyEnum classType) : base(classType)
{
// this will be executed after the base constructor completes
}
}
or by hard-coding the value (imagine that MyEnum contains a value Person
):
public class Person : Base
{
public Person() : base(MyEnum.Person)
{
// this will be executed after the base constructor completes
}
}
Note that you can have multiple constructors, so if the values should be instantiated with some defaults by derived classes, you could define a different protected
constructor to be used by derived classes. protected
ensures that it can only be called by derived classes, and not by anyone calling new Base(...)
:
public class Base
{
private readonly MyEnum _classType;
public Base(MyEnum classType)
{
_classType = classType;
}
protected Base()
{
_classType = classType.Derived;
}
}
public class Person : Base
{
public Person() : base()
{
// this will be executed after the base constructor completes
}
}
There’s no relationship between the number of arguments in the base and derived classes. There exists only a simple requirement: a derived constructor must call (and satisfy any argument requirements) its base class constructor where the base class constructor takes arguments, or there is more than one.
In your specific example, you probably intended this:
public Stat(bool _Status, int _Cid, int _Bal, String _Cname) : base(_Cid, _Bal, _Cname)
As a side note, naming arguments _Cid
is a bit strange. The prefix _
typically indicates that it is a private field in a class. Normal C# conventions use camel case (camelCaseArgumentName
) for method arguments.