473,503 Members | 12,103 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 1716
It is called an "explicit interface implementation", and you can do it like
this.

interface IInterface
{
IAnotherInterface Whatever ( ) ;
}

class AnotherInterfaceImplementor
: IAnotherInterface
{
....
}

class Implementor
: IInterface
{

public AnotherInterfaceImplementor Whatever ( )
{
return new AnotherInterfaceImplementor();
}

IAnotherInterface IInterface.Whatever ( )
{
return Whatever();
}
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jarmo Muukka" <jm*****@nospamto.hotmail.com> wrote in message
news:OC**************@TK2MSFTNGP09.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.Method()

{

return this.Method();

}

public Foo Method()

{

return new Foo();

}

}

Hope that helps.

- Markus

"Jarmo Muukka" <jm*****@nospamto.hotmail.com> schrieb im Newsbeitrag
news:OC**************@TK2MSFTNGP09.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
{
IAnotherInterface Whatever ( ) ;
}

class AnotherInterfaceImplementor
: IAnotherInterface
{
...
}

class Implementor
: IInterface
{

public AnotherInterfaceImplementor Whatever ( )
{
return new AnotherInterfaceImplementor();
}

IAnotherInterface IInterface.Whatever ( )
{
return Whatever();
}
}

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jarmo Muukka" <jm*****@nospamto.hotmail.com> wrote in message
news:OC**************@TK2MSFTNGP09.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
2639
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
4236
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...
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
2840
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...
16
3178
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
4769
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...
45
18764
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...
15
1236
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...
4
5967
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...
0
7098
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...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
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...
0
7470
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...
0
5604
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,...
1
5026
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...
0
4696
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...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
405
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...

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.