By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,775 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,775 IT Pros & Developers. It's quick & easy.

Implementing interface, returning subclass

P: n/a
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.)

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Apr 4 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 4 '06 #2

P: n/a

"David Thielen" <th*****@nospam.nospam> wrote in message
news:1D**********************************@microsof t.com...
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
Apr 4 '06 #3

P: n/a
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?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

"Mattias Sjögren" wrote:
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 4 '06 #4

P: n/a
David,
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.


Did you try the code I posted? It doesn't use the explicit keyword,
and I didn't mean to imply that you should. I'm talking about explicit
interface implementation:

http://msdn.microsoft.com/library/en...onTutorial.asp
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.