473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1705
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...
0
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...

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.