Implementing interface, returning subclass

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi;

I am writing a class that implements IDbConnection. The i/f defines a methof
BeginTransaction() that returns an IDbTransaction. I want to define this as
returning a DbTransaction (as DbConnection does). It makes sense to me that
this is legal as DbTransaction implements IDbTransaction so it does meet the
contract of returning an IDbTransaction.

But it won't compile - how do I get this to work? (There must be a way as
DbConnection does it.)
 
David,
But it won't compile - how do I get this to work? (There must be a way as
DbConnection does it.)

Something like this, using explicit implementation

IDbTransaction IDbConnection.BeginTransaction()
{
return BeginTransaction();
}

public DbTransaction BeginTransaction()
{
// ...
}


Mattias
 
David Thielen said:
Hi;

I am writing a class that implements IDbConnection. The i/f defines a
methof
BeginTransaction() that returns an IDbTransaction. I want to define this
as
returning a DbTransaction (as DbConnection does). It makes sense to me
that
this is legal as DbTransaction implements IDbTransaction so it does meet
the
contract of returning an IDbTransaction.

But it won't compile - how do I get this to work? (There must be a way as
DbConnection does it.)


You can return a DbTransaction if you like, but the method must be declared
as returning IDbTransaction. And you can always use explicit interface
implementation to hide all of th IDbConnection interface methods.

David
 
Hi;

I tried explicit but the error message says it is for operators only and I
need this for a property. And it won't let me have two properties with the
same name and differing only in return type.

Any other approaches?
 

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

Back
Top