473,545 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to change return type of a method in an implementation of an interface?

Hello,

I have an interface which has a method which returns an interface. I would
like to return a real type in implemented class.

interface IFoo
{
....
}

interface ISomething
{
IFoo Method();
}

class Foo : IFoo
{
....
}

class Something : ISomething
{
IFoo Method()
{
return new Foo();
}
}

I would like to return Foo as a return type of Method. Is it possible?

Microsoft has done it, but how? They have a SqlDataReader class which
implements IDataReader and they have a SqlCommand which implements
IDbCommand. IDbCommand has a method ExecuteReader which returns IDataReader,
but ExecuteReader of SqlCommand returns SqlDataReader. How did they do this?

JMu
Jul 21 '05 #1
3 1720
It is called an "explicit interface implementation" , and you can do it like
this.

interface IInterface
{
IAnotherInterfa ce Whatever ( ) ;
}

class AnotherInterfac eImplementor
: IAnotherInterfa ce
{
....
}

class Implementor
: IInterface
{

public AnotherInterfac eImplementor Whatever ( )
{
return new AnotherInterfac eImplementor();
}

IAnotherInterfa ce IInterface.What ever ( )
{
return Whatever();
}
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jarmo Muukka" <jm*****@nospam to.hotmail.com> wrote in message
news:OC******** ******@TK2MSFTN GP09.phx.gbl...
Hello,

I have an interface which has a method which returns an interface. I would
like to return a real type in implemented class.

interface IFoo
{
...
}

interface ISomething
{
IFoo Method();
}

class Foo : IFoo
{
...
}

class Something : ISomething
{
IFoo Method()
{
return new Foo();
}
}

I would like to return Foo as a return type of Method. Is it possible?

Microsoft has done it, but how? They have a SqlDataReader class which
implements IDataReader and they have a SqlCommand which implements
IDbCommand. IDbCommand has a method ExecuteReader which returns
IDataReader, but ExecuteReader of SqlCommand returns SqlDataReader. How
did they do this?

JMu

Jul 21 '05 #2
You need to use explicit interface implementation. Try the following:

public interface IFoo

{

}

public interface ISomething

{

IFoo Method();

}

public class Foo: IFoo

{}

public class Something: ISomething

{

IFoo ISomething.Meth od()

{

return this.Method();

}

public Foo Method()

{

return new Foo();

}

}

Hope that helps.

- Markus

"Jarmo Muukka" <jm*****@nospam to.hotmail.com> schrieb im Newsbeitrag
news:OC******** ******@TK2MSFTN GP09.phx.gbl...
Hello,

I have an interface which has a method which returns an interface. I would
like to return a real type in implemented class.

interface IFoo
{
...
}

interface ISomething
{
IFoo Method();
}

class Foo : IFoo
{
...
}

class Something : ISomething
{
IFoo Method()
{
return new Foo();
}
}

I would like to return Foo as a return type of Method. Is it possible?

Microsoft has done it, but how? They have a SqlDataReader class which
implements IDataReader and they have a SqlCommand which implements
IDbCommand. IDbCommand has a method ExecuteReader which returns IDataReader, but ExecuteReader of SqlCommand returns SqlDataReader. How did they do this?
JMu

Jul 21 '05 #3
Thank you Dennis and Markus (another answer).

JMu

"Dennis Myrén" <de****@oslokb. no> wrote in message
news:%I******** ********@news4. e.nsc.no...
It is called an "explicit interface implementation" , and you can do it
like this.

interface IInterface
{
IAnotherInterfa ce Whatever ( ) ;
}

class AnotherInterfac eImplementor
: IAnotherInterfa ce
{
...
}

class Implementor
: IInterface
{

public AnotherInterfac eImplementor Whatever ( )
{
return new AnotherInterfac eImplementor();
}

IAnotherInterfa ce IInterface.What ever ( )
{
return Whatever();
}
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jarmo Muukka" <jm*****@nospam to.hotmail.com> wrote in message
news:OC******** ******@TK2MSFTN GP09.phx.gbl...
Hello,

I have an interface which has a method which returns an interface. I
would like to return a real type in implemented class.

interface IFoo
{
...
}

interface ISomething
{
IFoo Method();
}

class Foo : IFoo
{
...
}

class Something : ISomething
{
IFoo Method()
{
return new Foo();
}
}

I would like to return Foo as a return type of Method. Is it possible?

Microsoft has done it, but how? They have a SqlDataReader class which
implements IDataReader and they have a SqlCommand which implements
IDbCommand. IDbCommand has a method ExecuteReader which returns
IDataReader, but ExecuteReader of SqlCommand returns SqlDataReader. How
did they do this?

JMu


Jul 21 '05 #4

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

Similar topics

8
2644
by: Martin Stettner | last post by:
Hi, I would like to use covariant return types in mutual dependent classes like: class IB; class IA { virtual IB* getIB() = 0; }; class IB{ virtual IA* getIA() = 0;
13
4239
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public abstract new System.Data.IDbConnection Connection }
3
340
by: Jarmo Muukka | last post by:
Hello, I have an interface which has a method which returns an interface. I would like to return a real type in implemented class. interface IFoo { .... }
1
2846
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive types and of more elaborate classes implementing IXmlSerializable. The resulting WSDL file for the webservice has two separate schemas in its <types>...
16
3180
by: Bob Hairgrove | last post by:
Consider the classic clone() function: class A { public: virtual ~A() {} virtual A* clone() const = 0; }; class B : public A { public:
9
4775
by: hufaunder | last post by:
I have a class "TestSuper" that implements the interface "TestBase". The interface has a property of type "ReturnType". The class "TestSuper" does not return "ReturnType" but a derivation "ReturnSuper". This gives the following compile error due to a bad return type: TestSuper does not implement interface member TestBase.Func....
45
18794
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
15
1242
by: Sunburned Surveyor | last post by:
I'm a Java developer in the process of writing a class library in C#, so please bear with me. I'm trying to write a method that can return a generic Object OR a Decimal value. In Java I would just have the method return an Object, since all Java primitive data types have Object wrappers like Integer and Double. If I have a method...
4
5970
by: chambersdon | last post by:
I need to create an abstract class but have the method in the derived class differ by the return type. This is my code: //Abstract class public abstract class DataItem { public abstract DbDataReader getRecord(); } //Derived Class public class DistributionCode:DataItem
0
7479
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...
0
7773
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...
0
5987
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...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4962
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...
0
3468
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...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.