472,958 Members | 2,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Inheritance and overriding

Hello all,

Why this piece of code doesn't compile?

public class FStorage
{}

public class FSExcel : FStorage
{}

public abstract class IResultLine
{
public abstract void ToExcelLine(FStorage storage);
}
public class Foo
{
internal class ResultLine : IResultLine
{
public sealed override void ToExcelLine(FSExcel storage) {}
}
}
I have the next errors:

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(AddIn.View.FileStorage.FSExcel) ':
no suitable method found to override

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'

Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?

Thanks in advance

Mike
Jun 27 '08 #1
9 1294
Michael wrote:
public abstract void ToExcelLine(FStorage storage);
public sealed override void ToExcelLine(FSExcel storage) {}
My guess is FSExcel and FStorage aren't the same.

Chris.
Jun 27 '08 #2
On Apr 16, 12:20 pm, Michael <michael.de...@gmail.comwrote:

<snip>
'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(AddIn.View.FileStorage.FSExcel) ':
no suitable method found to override

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'

Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?
To override a method, you have to specify *exactly* the same signature
(in terms of types and parameter modifiers; the parameter names can be
different).

Jon
Jun 27 '08 #3
Michael wrote:
public abstract void ToExcelLine(FStorage storage);
public sealed override void ToExcelLine(FSExcel storage) {}

I have the next errors:

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(A
ddIn.View.FileStorage.FSExcel)': no suitable method found to override

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'
Because the signature of both methods is not the same. What you are
trying to do is called "covariance", and C# does not support that for
method overriding. It is only supported for arrays and delegates (since
C# 2.0).

--
Rudy Velthuis http://rvelthuis.de

"Far too many development shops are run by fools who succeed
despite their many failings." -- Brion L. Webster
Jun 27 '08 #4
On Apr 16, 3:20*pm, Michael <michael.de...@gmail.comwrote:
Hello all,

Why this piece of code doesn't compile?

public class FStorage
{}

public class FSExcel : FStorage
{}

public abstract class IResultLine
{
* * public abstract void ToExcelLine(FStorage storage);

}

public class Foo
{
internal class ResultLine : IResultLine
{
* * public sealed override void ToExcelLine(FSExcel storage) {}

}
}

I have the next errors:

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(AddIn..*View.FileStorage.FSExce l)':
no suitable method found to override

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'

Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?

Thanks in advance

Mike
Hi,

public abstract void ToExcelLine(FStorage storage);
public sealed override void ToExcelLine(FSExcel storage) {}

are not the same, they need to have the same signature.
Frankly what sense it has to declare a method virtual (which you do by
declaring it abstract) and later declared it as sealed ?
Jun 27 '08 #5
On Apr 16, 1:30 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:

<snip stuff about the signature>
Frankly what sense it has to declare a method virtual (which you do by
declaring it abstract) and later declared it as sealed ?
That makes perfect sense to me. It's often entirely appropriate to say
"I don't want anyone overriding this method any further" but still
having it abstract in the base class.

It's similar to implementing an interface but marking the implementing
method as sealed, which is common in the Dispose pattern.

Personally I'd prefer to seal the whole derived class if possible, but
I have relatively extreme views on inheritance :)

Jon
Jun 27 '08 #6
Ignacio Machin ( .NET/ C# MVP ) wrote:

public abstract void ToExcelLine(FStorage storage);
public sealed override void ToExcelLine(FSExcel storage) {}
Frankly what sense it has to declare a method virtual (which you do by
declaring it abstract) and later declared it as sealed ?
It can make sense, on two grounds: correctness and performance.

If the descendant which overrides and seals the method needs to rely on
the implementation not changing for the rest of the implementation of
that descendant, it may seal the method. Further descendants won't be
able to override that particular method.

For the ancestor declaring the method, and intermediate ancestors,
virtual dispatch for the method will still work as expected.

In terms of performance: if a value that is statically known to be of a
type which declares a method to be sealed, or a descendant of a type
which declares a method to be sealed, virtual dispatch doesn't need to
be performed. A static call may be made instead.

-- Barry

--
http://barrkel.blogspot.com/
Jun 27 '08 #7
On 16 avr, 21:46, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Apr 16, 12:20 pm, Michael <michael.de...@gmail.comwrote:

<snip>
'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(AddIn.View.FileStorage.FSExcel) ':
no suitable method found to override
'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'
Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?

To override a method, you have to specify *exactly* the same signature
(in terms of types and parameter modifiers; the parameter names can be
different).

Jon
OK, thanks for your reply, I didn't know that covariance couldn't be
applied here. Anyway I think that's a strange limitation, because I
don't see what would be a problem for the compiler to deduce that
FSExcel inherits from FStorage...

So I'll have to keep a cast to FSExcel in my function.
Jun 27 '08 #8
On Apr 16, 11:23 pm, Michael <michael.de...@gmail.comwrote:

<snip>
OK, thanks for your reply, I didn't know that covariance couldn't be
applied here. Anyway I think that's a strange limitation, because I
don't see what would be a problem for the compiler to deduce that
FSExcel inherits from FStorage...

So I'll have to keep a cast to FSExcel in my function.
In this case it wouldn't be valid even if covariance/contravariance
*did* apply. An override should apply to *all* calls to the method -
so with covariance/contravariance you would be able to override with
*less* specific parameters and a *more* specific return type. You're
trying to override with a *more* specific parameter.

What would you expect to happen if someone called ToExcelLine with an
FStorage which *wasn't* an FSExcel?

Jon
Jun 27 '08 #9
Michael wrote:
On 16 avr, 21:46, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
>On Apr 16, 12:20 pm, Michael <michael.de...@gmail.comwrote:

<snip>
>>'AddIn.ListOfControls.DerniersRapportsVisite.Res ultLine_.ToExcelLine(AddIn.View.FileStorage.FSExce l)':
no suitable method found to override
>>'AddIn.ListOfControls.DerniersRapportsVisite.Res ultLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.F ileStorage.FStorage)'
>>Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?

To override a method, you have to specify *exactly* the same
signature (in terms of types and parameter modifiers; the parameter
names can be different).

Jon

OK, thanks for your reply, I didn't know that covariance couldn't be
applied here. Anyway I think that's a strange limitation, because I
don't see what would be a problem for the compiler to deduce that
FSExcel inherits from FStorage...

So I'll have to keep a cast to FSExcel in my function.
The problem is rather that the interface contract from the base class says
that you will accept any FStorage. You're now only providing an
implementation for the subset of FStorage objects which as FSExcel-derived.
This breaks the contract, and the compiler tells you so.
Jun 27 '08 #10

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

Similar topics

4
by: Dan Bullok | last post by:
I have a couple of classes: class Base: ... class Sub(Base): ... I want to subclass Base and Sub, i.e. define classes: class MyBase(Base):
1
by: Tony Johansson | last post by:
Hello! Private inheritance is sometimes called implementation inheritance. If you use this private inheritance how is with the usage of overriding then. Is overriding used less often when...
0
by: Ian | last post by:
I've got this problem with overriding attributes on an inheritance chain where the XmlSerializer is concerned. For example: public class A { private string aWord = String.Empty(); public...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
2
by: Emmanuel | last post by:
Hi, I'm working on a c# web app and require having some code which runs in the page Load event of each page and to be reusable in other web apps. So i decided to use a Class Library which...
6
by: tshad | last post by:
I am playing with Inheritance and want to make sure I understand it. I have the following Classes: ******************************************* Public Class AuthHeader:Inherits SoapHeader Public...
4
by: vivekian | last post by:
Hi, Have this following hierarchy which am implementing for a networking program. The base class 'ASocket' is the base class from which 'AListener' and 'ATalker' inherit . None of the functions...
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
6
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.