Re: C# inheritance broken?



Just a word of advice.

When posting on a newgroup of c#, don't come out with statements like 'c#
inheritence broken' just because you don't know the c# way of doing
something you are used to.

Better to say i can do this in c++ but can't seem to find a comparative
solution in c#, here is my problem etc etc. You'll find you get a much
better response.



<groups@xxxxxxxxxxxxx> wrote in message
news:1169831207.816914.59560@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
C# is an impressive language...but it seems to have one big limitation
that, from a C++ background, seems unacceptable.

Here's the problem:

I have a third-party Document class. (This means I can't change the
Document class.) I want to extend this (inherit from Document) as
MyDocument, adding new events and application-specific methods and
properties.

I submit that this can't be done in C#.

Consider this example:

class MyDocument : public Document

This doesn't work because the only way to open an existing Document is
using the static method Document.Load(string FileName), which returns a
Document object. There doesn't seem to be any way to convert or cast a
Document object to a MyDocument object. There is therefore no way to
"Load" a MyDocument object!

From reading LOTS of posts on this group, the standard answers are:

1. "Wrap" the object, as follows:
class MyDocument
{
protected Document TheDocument;
}

But...this means that I have to wrap EACH of the hundreds of methods
and properties needed to manipulate the Document object. This is
clearly a case for inheritance, to allow the base class properties and
methods to be automatically available, with enhancements from the
derived class.

2. In MyDocument.Load, call Document.Load, and then COPY all of
Document's members to MyDocument.

BUT, this is prohibitive because COPYING all of Document's members is
prohibitive in terms of run time, and also in terms of development
effort (lines of code, and therefore, potential bugs).

In C++, I would do something like this:

class MyDocument : public Document
{
public static MyDocument Load(string FileName)
{
return (MyDocument)Document.Load(FileName);
}
}

So my conclusion is that C# inheritance is broken (in practice) because
of its strict type-checking, and no allowance for such a common
scenario.

Did I miss something?



.



Relevant Pages

  • C# inheritance broken?
    ... class MyDocument: public Document ... Document object to a MyDocument object. ... So my conclusion is that C# inheritance is broken because ...
    (microsoft.public.dotnet.languages.csharp)
  • C# inheritance broken?
    ... class MyDocument: public Document ... Document object to a MyDocument object. ... So my conclusion is that C# inheritance is broken because ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# inheritance broken?
    ... class MyDocument: public Document ... Document object to a MyDocument object. ... Document's members to MyDocument. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# inheritance broken?
    ... to create a new Document object. ... if they design one that requires you to ... don't have any problem creating a new MyDocument object when I need it; ... public static Document Load(string FileName); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# inheritance broken?
    ... You also assume that a common scenario in C++ would be common in C#, ... class MyDocument: public Document ... Document object to a MyDocument object. ...
    (microsoft.public.dotnet.languages.csharp)

Loading