Override a method and return a different type

C

chambersdon

I need to create an abstract class but have the method in the derived
class differ by the return type.
This is my code:
//Abstract class
public abstract class DataItem
{
public abstract DbDataReader getRecord();
}
//Derived Class
public class DistributionCode:DataItem
{
public override SQLDataReader getRecord()
{

SqlDataReader r = getReader();
return r;
}

My problem is the return type, SQLDataReader in the derived class.
This causes a compiler error that states ‘the return type must be
DbDataReader’.

Microsoft seems to be doing this ExecuteReader method of the
SqlCommand class.

The SQLCommand class inherits from DBCommand (abstract class).
The DBCommand class has a method called ExecuteReader that returns a
DbDataReader.
The the overridden ExecuteReader method, in SQLCommand returns a
SQLDataReader, not the DbDataReader from its base class.
 
J

Jon Skeet [C# MVP]

I need to create an abstract class but have the method in the derived
class differ by the return type.

C# doesn't (yet) support covariant return types, I'm afraid. If you're
implementing an interface you can use explicit interface implementation
to get round this, but for straight overriding of a virtual/abstract
method, you're out of luck.
 
C

chambersdon

C# doesn't (yet) support covariant return types, I'm afraid. If you're
implementing an interface you can use explicit interface implementation
to get round this, but for straight overriding of a virtual/abstract
method, you're out of luck.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

How is Microsoft doing this in the SQLCommand object I explained
above?
Am i misunderstanding something?
 
J

Jeroen Mostert

How is Microsoft doing this in the SQLCommand object I explained
above?

They're not. SqlCommand.ExecuteReader() does not override
DbCommand.ExecuteReader() -- neither of these methods is virtual. It's legal
for a derived type to declare a method that matches a base class method and
differs only in return type (to do this in C#, you must use the "new"
keyword). These methods will not be called virtually -- if you have a
DbCommand reference, calling .ExecuteReader() on it will only ever invoke
DbCommand.ExecuteReader(), regardless of the actual type of the referred object.

..ExecuteReader() defers to the virtual .ExecuteDbDataReader(), which has the
same signature in both classes.
 
J

JTC^..^

I need to create an abstract class but have the method in the derived
class differ by the return type.
This is my code:
//Abstract class
    public abstract class DataItem
    {
        public abstract DbDataReader getRecord();
    }
//Derived Class
    public class DistributionCode:DataItem
    {
        public override SQLDataReader getRecord()
        {

            SqlDataReader r = getReader();
            return r;
        }

My problem is the return type, SQLDataReader in the derived class.
This causes a compiler error that states ‘the return type must be
DbDataReader’.

Microsoft seems to be doing this ExecuteReader method of the
SqlCommand class.

The SQLCommand class inherits from DBCommand (abstract class).
The DBCommand class has a method called ExecuteReader that returns a
DbDataReader.
The the overridden ExecuteReader method, in SQLCommand returns a
SQLDataReader, not the DbDataReader from its base class.

Look at the IDataReader interface. It may help you derive a
solution ... http://msdn.microsoft.com/en-us/library/system.data.idatareader_members.aspx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top