Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The program does not return the derived class results

I am writing a script service (ScriptExecuter) which execute scripts. the ScriptExecuter class contains two virtual method ExecuteSQL and CompileCSharp.

Here is the code:

public class ScriptExecuter
{
    public virtual bool ExecuteSQL(string query)
    {
        return false;
    }
    public virtual bool CompileCSharp(string code)
    {
        return false;
    }
}
public class SQLExecuter : ScriptExecuter
{
    public override bool ExecuteSQL(string query)
    {
        return true;
    }

}
public class CSharpCompiler : ScriptExecuter
{
    public override bool CompileCSharp(string query)
    {
        return true;
    }

}

And here is my main method code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public class Program
{
    static void Main(string[] args)
    {
        var scriptExecuter=new ScriptExecuter();
        var result = scriptExecuter.ExecuteSQL("SELECT * FROM Table1");
        Console.WriteLine(result);
    }

}

The output is false. I want the value of the derived classes.

my question is: How can ScriptExecuter return its derived class return value?

>Solution :

var scriptExecuter=new ScriptExecuter();

You are creating an instance of the base class, if you want an instance of the derived class, you have to create an instance of it.

so replace

var scriptExecuter=new ScriptExecuter();

with

SQLExecuter scriptExecuter = new SQLExecuter();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading