Asking again: 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 method
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.

I tried explicit/implicit but got an error message saying that could only be
used with operators.
 
It makes sense to me that
this is legal as DbTransaction implements IDbTransaction so it does meet the
contract of returning an IDbTransaction.
Does not. An interface variable has no knowledge of methods declared outside
of the interface class - for instance in the derived class. So it seems to
me that (and I haven't seen your code) you are attempting to access a method
whose signature does not sit inside the interface class proper.

--
Warm Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley 2006
Blog: http://msmvps.com/blogs/Alvin/
 
Could you post some code so that we can see what you're doing? It's
kind of hard to visualize....
 
HI,

If you post the code as well as the compiler error you will get a much
precise answer.
 
Ignacio Machin ( .NET/ C# MVP ) said:
HI,

If you post the code as well as the compiler error you will get a much
precise answer.

What Ignacio said; this is the second time you've asked about code that
won't compile, but haven't provided the code. There are some very bright
people haere, but AFAIK no mind-readers :-)
 
Mike Schilling said:
What Ignacio said; this is the second time you've asked about code that
won't compile, but haven't provided the code. There are some very bright
people haere, but AFAIK no mind-readers :-)

good point - here it is. Thanks - dave

public class WrCommand : IDisposable, IDbCommand
{
private DbProviderFactory provider;
private DbCommand cmd;
....
public IDbConnection Connection
{
get { return cmd.Connection; }
set { cmd.Connection = (DbConnection) value; }
}
}

I would like the property to be (IDbConnection -> DbConnection):
public DbConnection Connection
{
get { return cmd.Connection; }
set { cmd.Connection = (DbConnection) value; }
}

DbCommand does this. It is declared as:
public abstract class DbCommand : Component, IDbCommand, IDisposable

And it has the property (DbConnection, not IDbConnection):
public DbConnection Connection { get; set; }

Any idea how it pulls this off?

thanks - dave
 
Any idea how it pulls this off?

The answer is the same as it was before - it uses explicit interface
implementation. To quote Mattias:

<quote>
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-
us/csref/html/vcwlkExplicitInterfaceImplementationTutorial.asp
</quote>
 
Hi;

Ok, I tried this a couple of different ways including:
DbConnection IDbConnection.Connection
{
get { return cmd.Connection; }
set { cmd.Connection = (DbConnection) value; }
}

but each gives me a compile error. I looked at the example and it seems the
example is the oppisate of this (I think) where it lets the class act like
the i/f. But I want to turn the i/f into a class that implements it.

What am I not getting?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
 
David Thielen said:
Ok, I tried this a couple of different ways including:
DbConnection IDbConnection.Connection
{
get { return cmd.Connection; }
set { cmd.Connection = (DbConnection) value; }
}

but each gives me a compile error. I looked at the example and it seems the
example is the oppisate of this (I think) where it lets the class act like
the i/f. But I want to turn the i/f into a class that implements it.

What am I not getting?

You need to have two properties - one implementing the interface
explicitly (and returning the interface), and one just declaring the
property, and declaring it to return the concrete type.
 
David Thielen said:
Hi;

Ok, I tried this a couple of different ways including:
DbConnection IDbConnection.Connection
{
get { return cmd.Connection; }
set { cmd.Connection = (DbConnection) value; }
}

You need 2 Connection properties on your class that is implementing
IDBCommand. The signature on the first needs to be IDbConnection
IDbCommand.Connection to "satisfy" the interface. The second would be public
MyDbConnection Connection. Both properties will return an instance of your
class that implements the IDbConnection interface.

SP
 
David Thielen said:
That did it - thank you. Weird that I have to remove the public from the i/f
one and yet it's still public...

It's sort of public, and sort of private - that's just a quirk of how
explicit interface implementation works.
 

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