Re: C# inheritance broken?





On Jan 26, 11:36 am, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
wrote:
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.com> wrote in
messagenews:%231AcH4XQHHA.496@xxxxxxxxxxxxxxxxxxxxxxx



Hi,

"Mythran" <kip_pot...@xxxxxxxxxxx> wrote in message
news:e8FyEvXQHHA.4896@xxxxxxxxxxxxxxxxxxxxxxx
|
|
| <gro...@xxxxxxxxxxxxx> wrote in message
|news:1169837076.386389.190580@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
| > Ignacio,
| >
| > Thank you for your thoughts.
| >
| > The Document class does have a public default constructor, and it is
| > not sealed. I have no problem creating a "new" Document object, or a
| > "new" MyDocument object, for that matter. What I can't do is
"convert"
| > an existing Document object (such as the one returned by
Document.Load)
| > into a MyDocument.

As I said in another post, giving the struct you are presenting you would
have the same issue no matter what language you use. You cannot treat a
base
class as a derived class, you can do the opposite though.

In your case you will have to implement your own Load method. I would
first
check though if Document provide a Load method instance. or maybe a Clone
method

--
Ignacio Machin
machin AT laceupsolutions comThis isn't a C# issue. It is a dotNet issue as I have run into the same
issue of inheriting a base class (in my case, VB 2005 and StringCollection)
and providing a method for the base class object to become the object at the
heart of the derived class. This is a design limitation in the framework
itself. From a business logic perspective, it should be doable.

For the record, no language that I know of lets you do this. C++ allows
you to pretend to do it via an unsafe cast, but you're not really
getting a derived object, just playing with a base class object as
though it were a derived object, which is fodder for disaster unless
you keep careful track of what you're doing.

You can't use a base class instance as though it were a derived class
instance because the latter may have more fields than the former, and
so the base class instance doesn't have enough space to store the state
of the derived class.

There _are_ various strategies for using a base class instance as a
model for creating a derived class. Most of them, however, require some
coding. There's no "automatic" way of copying state from one instance
to another, which might be a nice addition.

.



Relevant Pages

  • Re: C# inheritance broken?
    ... class as a derived class, you can do the opposite though. ... Ignacio Machin ... and providing a method for the base class object to become the object at ... You can't use a base class instance as though it were a derived class ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Application.Run() problem
    ... still am very much confused about the paint event handler. ... It most obviously should be the derived class, ... It seems that you're not understanding how inheritance works. ... which ensures that the base class uses the same ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Very Confused on Page 33
    ... On page 33 Bruce is explaining inheritance. ... > base class you can also send to objects of the derived class. ... The programmatic difference enables the subclass ...
    (comp.lang.java.help)
  • Re: attribute or subclass
    ... With inheritance the derived class code ... reference in place of the base class reference. ...
    (comp.object)
  • Re: Page_load not called in base class
    ... > Which derived class is hiding the base class ... Note that the base class already has a method called ... as Overrideable, Overrides, or MustOverride is available to (and, in fact, ... I'm not sure why implicitly hiding or shadowing a member in a base class is ...
    (microsoft.public.dotnet.framework.aspnet)