473,725 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asking again: Implementing interface, returning subclass

Hi;

I am writing a class that implements IDbConnection. The i/f defines a method
BeginTransactio n() 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.

--
thanks - dave
david_at_windwa rd_dot_net
http://www.windwardreports.com

Apr 7 '06 #1
11 1734
>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/
-------------------------------------------------------

"David Thielen" <th*****@nospam .nospam> wrote in message
news:BC******** *************** ***********@mic rosoft.com... Hi;

I am writing a class that implements IDbConnection. The i/f defines a method BeginTransactio n() 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.

--
thanks - dave
david_at_windwa rd_dot_net
http://www.windwardreports.com

Apr 7 '06 #2
Could you post some code so that we can see what you're doing? It's
kind of hard to visualize....

Apr 7 '06 #3
HI,

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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"David Thielen" <th*****@nospam .nospam> wrote in message
news:BC******** *************** ***********@mic rosoft.com...
Hi;

I am writing a class that implements IDbConnection. The i/f defines a
method
BeginTransactio n() 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.

--
thanks - dave
david_at_windwa rd_dot_net
http://www.windwardreports.com

Apr 7 '06 #4

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:et******** ******@TK2MSFTN GP05.phx.gbl...
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 :-)
Apr 7 '06 #5
"Mike Schilling" wrote:

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:et******** ******@TK2MSFTN GP05.phx.gbl...
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 :-)


good point - here it is. Thanks - dave

public class WrCommand : IDisposable, IDbCommand
{
private DbProviderFacto ry 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


Apr 7 '06 #6
David Thielen <th*****@nospam .nospam> wrote:

<snip>
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/vcwlkExplicitIn terfaceImplemen tationTutorial. asp
</quote>

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 7 '06 #7
Hi;

Ok, I tried this a couple of different ways including:
DbConnection IDbConnection.C onnection
{
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_windwa rd_dot_net
http://www.windwardreports.com

"Jon Skeet [C# MVP]" wrote:
David Thielen <th*****@nospam .nospam> wrote:

<snip>
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/vcwlkExplicitIn terfaceImplemen tationTutorial. asp
</quote>

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 7 '06 #8
David Thielen <th*****@nospam .nospam> wrote:
Ok, I tried this a couple of different ways including:
DbConnection IDbConnection.C onnection
{
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 7 '06 #9
SP

"David Thielen" <th*****@nospam .nospam> wrote in message
news:2C******** *************** ***********@mic rosoft.com...
Hi;

Ok, I tried this a couple of different ways including:
DbConnection IDbConnection.C onnection
{
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.Conn ection 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 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_windwa rd_dot_net
http://www.windwardreports.com

"Jon Skeet [C# MVP]" wrote:
David Thielen <th*****@nospam .nospam> wrote:

<snip>
> 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/vcwlkExplicitIn terfaceImplemen tationTutorial. asp
</quote>

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5075
by: Murat Tasan | last post by:
i am implementing a custom version of the java.util.Map interface. my custom version does some encryption stuff when making modifications to the map via one of the 4 modification methods (put, putAll, remove, and clear). in doing this, i would like to also use one of my own exception objects... so these 4 methods in the custom version should now also be defined with: throws EncryptionException because EncryptionException is not a...
11
2758
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do understand the objective. This example may represent an interface in need of a bit of refactoring, but it goes to demonstrate the basic idea as I understand it. http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
3
4506
by: Ivan Neganov | last post by:
Hi, I have a custom subclass of System.IO.Stream type. I wonder how to correctly implement the IDisposable pattern in this situation. The parent Stream type apparently uses explicit interface implementation, and I could not find a way for my child type to override parent's IDisposable.Dispose() method. The intention was to clean up child's resources first,
8
2974
by: hex | last post by:
Hi I make a class "MyClass" and this clas implements the Interface ICloneable. I want when I instance an object from MyClass and I call obj.Clone() it returns an object of MyClass type. for example: public class MyClasss : ICloneable { public void A();
5
1669
by: Colin McGuire | last post by:
Hi all, when I write the class below Private Class employee End Class and then add the line "Implements IVF" which is an interface I have written, the IDE modifies my code to display
4
1478
by: David Thielen | last post by:
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...
6
1773
by: Dave Booker | last post by:
I want to do something like this: public class Animal; public interface IZoo { List<Animal> Animals { get; } void Feed(Animal a); }
3
1632
by: Daniel Kraft | last post by:
Hi, I usually program in C++, but for a special project I do have to code in C (because it may be ported to embedded-like platforms where no C++ compiler exists). I do want to realize some kind of polymorphic behaviour, like this: I have some abstract base "class" Base, that is, a struct containing a vtable of function-pointers and possibly some common fields (but at the
8
2637
by: nickooooola | last post by:
Hello to all I'm about to write a simulator for a microcontroller in python (why python? because I love it!!!) but I have a problem. The registry of this processor are all 8 bit long (and 10 bit for some other strange register) and I need to simulate the fixed point behaviour of the register, and to access the single bit.
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.