1

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:

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?

Green
  • 73
  • 5
  • You use new `ScriptExecuter()` which creates an instance of the base class. If you use `new SQLExecuter()` you'll get the derived class. Then, later on, you can start to play around with factory methods and factory patterns… – Pontus Gagge Aug 25 '22 at 07:34
  • I suggest you read more about inheritance. You're way off track with this. – Johnathan Barclay Aug 25 '22 at 07:35
  • 1
    May I suggest you read up on Composition over Inheritance? basically using Inheritance has become code smell. You should make interfaces: ISqlExecutor ICSharpCompiler And then use these as private fields on your class, and then assign the fields from your constructor. This would allow you any number of behaviours, and allow you to compose your class with the behaviour needed at runtime. – Morten Bork Aug 25 '22 at 13:32

5 Answers5

1
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();
Morten Bork
  • 1,413
  • 11
  • 23
1

It does that cause you declared it as the base class ScriptExecuter.

If you change the declaration to ScriptExecuter scriptExecuter=new SQLExecuter();, then, it will still be a ScriptExecuter object but with the SQLExecuter implementation for that method.

E.g. code:

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

You have to use the SQLExecuter, if you want the SQLExecuter to be called.

var scriptExecuter =new SQLExecuter();
emilsteen
  • 496
  • 4
  • 14
0

You need to create an instance of derived class, not base class:

var scriptExecuter= new SQLExecuter

And this is an examply of polymorphism:

ScriptExecuter scriptExecuter = new SQLExecuter();

Polymorphism is when you can treat an object as a generic version of something, but when you access it, the code determines which exact type it is and calls the associated code.

StepUp
  • 36,391
  • 15
  • 88
  • 148
0

In your Main Method, You make an instance of ScriptExecuter class and refrence it there. that is your Base class. you have to make instance of your Desired Derived class. other way is to do this:

var scriptExecuter=new SQLExecuter();// you can put any other derived class instead of SQLExecuter()