C# Virtual method call in constructor - how to refactor?

Posted by Cristi Diaconescu on Stack Overflow See other posts from Stack Overflow or by Cristi Diaconescu
Published on 2012-10-03T15:28:05Z Indexed on 2012/10/03 15:37 UTC
Read the original article Hit count: 218

Filed under:
|
|

I have an abstract class for database-agnostic cursor actions. Derived from that, there are classes that implement the abstract methods for handling database-specific stuff.

The problem is, the base class ctor needs to call an abstract method - when the ctor is called, it needs to initialize the database-specific cursor.

I know why this shouldn't be done, I don't need that explanation!

This is my first implementation, that obviously doesn't work - it's the textbook "wrong way" of doing it. The overridden method accesses a field from the derived class, which is not yet instantiated:

public abstract class CursorReader
{
    private readonly int m_rowCount;
    protected CursorReader()
    {
         m_rowCount = CreateCursor(sqlCmd); //virtual call !
    }
    protected abstract int CreateCursor(string sqlCmd);
}

public class SqlCursorReader : CursorReader
{
    private SqlConnection m_sqlConnection;

    public SqlCursorReader(string sqlCmd, SqlConnection sqlConnection)
    {
        m_sqlConnection = sqlConnection;     //field initialized here
    }
    protected override int CreateCursor(string sqlCmd)
    {
        //uses not-yet-initialized member *m_sqlConnection*
        //so this throws a NullReferenceException
        var cursor = new CustomCursor(sqlCmd, m_sqlConnection); 
        return cursor.Count();
    }
}

I will follow up with an answer on my attempts to fix this...

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET