473,499 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Override a method and return a different type

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
{
public override SQLDataReader getRecord()
{

SqlDataReader r = getReader();
return r;
}

My problem is the return type, SQLDataReader in the derived class.
This causes a compiler error that states ‘the return type must be
DbDataReader’.

Microsoft seems to be doing this ExecuteReader method of the
SqlCommand class.

The SQLCommand class inherits from DBCommand (abstract class).
The DBCommand class has a method called ExecuteReader that returns a
DbDataReader.
The the overridden ExecuteReader method, in SQLCommand returns a
SQLDataReader, not the DbDataReader from its base class.
Oct 10 '08 #1
4 5965
<ch*********@hotmail.comwrote:
I need to create an abstract class but have the method in the derived
class differ by the return type.
C# doesn't (yet) support covariant return types, I'm afraid. If you're
implementing an interface you can use explicit interface implementation
to get round this, but for straight overriding of a virtual/abstract
method, you're out of luck.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 10 '08 #2
On Oct 10, 12:56*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
*<chambers...@hotmail.comwrote:
I need to create an abstract class but have the method in the derived
class differ by the return type.

C# doesn't (yet) support covariant return types, I'm afraid. If you're
implementing an interface you can use explicit interface implementation
to get round this, but for straight overriding of a virtual/abstract
method, you're out of luck.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
How is Microsoft doing this in the SQLCommand object I explained
above?
Am i misunderstanding something?

Oct 10 '08 #3
ch*********@hotmail.com wrote:
On Oct 10, 12:56 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
> <chambers...@hotmail.comwrote:
>>I need to create an abstract class but have the method in the derived
class differ by the return type.
C# doesn't (yet) support covariant return types, I'm afraid. If you're
implementing an interface you can use explicit interface implementation
to get round this, but for straight overriding of a virtual/abstract
method, you're out of luck.
How is Microsoft doing this in the SQLCommand object I explained
above?
They're not. SqlCommand.ExecuteReader() does not override
DbCommand.ExecuteReader() -- neither of these methods is virtual. It's legal
for a derived type to declare a method that matches a base class method and
differs only in return type (to do this in C#, you must use the "new"
keyword). These methods will not be called virtually -- if you have a
DbCommand reference, calling .ExecuteReader() on it will only ever invoke
DbCommand.ExecuteReader(), regardless of the actual type of the referred object.

..ExecuteReader() defers to the virtual .ExecuteDbDataReader(), which has the
same signature in both classes.

--
J.
Oct 10 '08 #4
On 10 Oct, 17:43, chambers...@hotmail.com wrote:
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
* * {
* * * * public override SQLDataReader getRecord()
* * * * {

* * * * * * SqlDataReader r = getReader();
* * * * * * return r;
* * * * }

My problem is the return type, SQLDataReader in the derived class.
This causes a compiler error that states ‘the return type must be
DbDataReader’.

Microsoft seems to be doing this ExecuteReader method of the
SqlCommand class.

The SQLCommand class inherits from DBCommand (abstract class).
The DBCommand class has a method called ExecuteReader that returns a
DbDataReader.
The the overridden ExecuteReader method, in SQLCommand returns a
SQLDataReader, not the DbDataReader from its base class.
Look at the IDataReader interface. It may help you derive a
solution ... http://msdn.microsoft.com/en-us/libr...r_members.aspx.
Oct 11 '08 #5

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

Similar topics

14
12103
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
5
6832
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window {...
7
6420
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
5
2579
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
11
19430
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
5
9566
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
15
3764
by: Cliff_Harker | last post by:
Why can't I do this in C# public class A { public A virtual whatever( A a ) { } } public class B : A
2
4034
by: Adriano Coser | last post by:
Hello. After I converted my .net code to the new VC2005 syntax I started to get C4490 on my ExpandableObjectConverter subclass overrides. The GetProperties method is no longer called by the...
3
1328
by: Tony Johansson | last post by:
Hello! I have noticed that you can write this override in two different ways. You can either write in this way public override string ToString() { return something; }
0
7128
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
7006
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...
1
6892
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
7385
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
5467
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
4917
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
3096
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...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
294
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.