I'm having trouble trying to "override" a method from an inherited interface but return a different data type than is defined by the interface. Here's an example to clarify:
public interface IDataReader {}
public interface IDbCommand
{
IDataReader ExecuteReader();
}
public class MyCommand : IDbCommand
{
public IDataReader ExecuteReader() {return new MyDataReader();} }
public class MyDataReader : IDataReader {}
class Test
{
static void Main(string[] args)
{
MyCommand cmd = new MyCommand();
MyDataReader dr = cmd.ExecuteReader();
}
}
When I compile this I get "Cannot implicitly convert type 'IDataReader' to 'MyDataReader'" at the "MyDataReader dr = ..." line. I use the IDbCommand/IDataReader as an example because I've seen it in the SqlClient. The SqlClient analog to "MyDataReader dr = ...," "SqlDataReader dr = ..." does not produce the same error when compiled. So my question is, how do I modify my code to avoid having to explicitly cast "cmd.ExecuteReader()" to MyDataReader?
Thanks,
Charlie
Charlie Pastre
Transition/1 MAS
cpastre@t1mas.com
public interface IDataReader {}
public interface IDbCommand
{
IDataReader ExecuteReader();
}
public class MyCommand : IDbCommand
{
public IDataReader ExecuteReader() {return new MyDataReader();} }
public class MyDataReader : IDataReader {}
class Test
{
static void Main(string[] args)
{
MyCommand cmd = new MyCommand();
MyDataReader dr = cmd.ExecuteReader();
}
}
When I compile this I get "Cannot implicitly convert type 'IDataReader' to 'MyDataReader'" at the "MyDataReader dr = ..." line. I use the IDbCommand/IDataReader as an example because I've seen it in the SqlClient. The SqlClient analog to "MyDataReader dr = ...," "SqlDataReader dr = ..." does not produce the same error when compiled. So my question is, how do I modify my code to avoid having to explicitly cast "cmd.ExecuteReader()" to MyDataReader?
Thanks,
Charlie
Charlie Pastre
Transition/1 MAS
cpastre@t1mas.com