W
W
wolverine7772021-11-01 12:21:15
C++ / C#
wolverine777, 2021-11-01 12:21:15

How to pull inherited methods from a class object?

Hello, I'm doing an assignment on polymorphism in one of the online courses and everything would be fine, but I ran into the problem (as it seems to me of the syntax) of how to extract inherited methods from an object in this situation:

public class DbCommand
    {
        public DbCommand(object DbConnection)
        {

            Console.WriteLine("Db_COMMAND is online...\n");

            if (DbConnection is null)
            {
                throw new ArgumentNullException(nameof(DbConnection));
            }
        }

        public void Executerrr()
        {

            /*

            Here must be an object that should "discover" methods OpenConnection() and CloseConnection()
            but I cannot figure out what kind of object should I put inside Execute() method...

            */
        
        }

    }


    public abstract class DbConnection
    {
     
        public string _ConnectionString { get; set; }
        public TimeSpan Timeout { get; set; }

        public DbConnection(string connectionString)
        {
            _ConnectionString = connectionString;
        }

        public abstract void OpenConnection();
        public abstract void CloseConnection();
    }


    public class SqlConnection : DbConnection
    {
        public SqlConnection(string connectionString) : base(connectionString)
        {
        }

        public override void CloseConnection()
        {
            Console.WriteLine("SQL connenction Closed");
        }

        public override void OpenConnection()
        {
            Console.WriteLine("SQL connenction OPEN");
        }
    }

    public class OracleConnection : DbConnection
    {
        public OracleConnection(string connectionString) : base(connectionString)
        {
        }

        public override void CloseConnection()
        {
            Console.WriteLine("ORACLE connenction Closed");
        }

        public override void OpenConnection()
        {
            Console.WriteLine("ORACLE connenction OPEN");
        }
    }

static void Main(string[] args)
        {
// simple example:

            var SQLconn = new SqlConnection("sql ready");
            var ORACLEconn = new OracleConnection("oracle ready");

            SQLconn.OpenConnection();
            SQLconn.CloseConnection();

            ORACLEconn.OpenConnection(); // works fine
            ORACLEconn.CloseConnection(); // works fine
            
// here's a more complicated example

            var start = new DbCommand(new OracleConnection("oracle ready"));

            start.Executerrr();
}


It is necessary for me that if I give an object of the OracleConnection class - when the method is launched Executerrr(), the methods for the OracleConnection are executed and, accordingly, if the SqlConenction - the phrases for Sql are displayed. It just object DbConnectiondoes not see abstract methods (and according to my calculations, it seems like it should)))).

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AndromedaStar, 2021-11-01
@AndromedaStar

You must explicitly specify the class.

public DbCommand(object DbConnection) -> public DbCommand(DbConnection DbConnection)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question