Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does deriving from a class cause an error about the parent constructor?

My current code looks like this

this.actHandler = new MainAct(reqParams[2], new DatabaseHandler());

RegisterAct regAct = new RegisterAct();

return regAct.RegisterAction();

MainAct class

class MainAct
{
    protected DatabaseHandler dbh;

    protected MySqlConnection db;

    protected MySqlDataReader reader;

    protected MySqlCommand cmd;

    protected string param;

    public MainAct(string param, DatabaseHandler dbHandler)
    {
        this.param = param;
        this.dbh = dbHandler;
        this.db = this.dbh.ConnectDatabase();
    }
}

RegisterAct class

class RegisterAct : MainAct
{
    public string RegisterAction()
    {

    }
}

And Im getting the following error :

BloodServer.act.MainAct doesnt contain a constructor with 0 args

This is in the Parent class, which I thought I replaced.
Does the Parent constructor still get called? Is there a way to change this?

like image 454
Raggaer Avatar asked Feb 02 '26 05:02

Raggaer


1 Answers

In your RegisterAct class, you need to add a constructor which calls the base constructor with the right parameters:

class RegisterAct : MainAct
{
    public RegisterAct(string param, DatabaseHandler dbHandler) : base(param, dbHandler)
    {
    }

    // Other methods/code here
}
like image 76
Tim Copenhaver Avatar answered Feb 04 '26 23:02

Tim Copenhaver