Re: C# inheritance broken?
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 26 Jan 2007 16:16:02 -0800
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
--issue of inheriting a base class (in my case, VB 2005 and StringCollection)
Ignacio Machin
machin AT laceupsolutions comThis isn't a C# issue. It is a dotNet issue as I have run into the same
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.
.
- Follow-Ups:
- Re: C# inheritance broken?
- From: Andy
- Re: C# inheritance broken?
- From: Michael D. Ober
- Re: C# inheritance broken?
- References:
- C# inheritance broken?
- From: groups
- Re: C# inheritance broken?
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: C# inheritance broken?
- From: groups
- Re: C# inheritance broken?
- From: Mythran
- Re: C# inheritance broken?
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: C# inheritance broken?
- From: Michael D. Ober
- C# inheritance broken?
- Prev by Date: Re: Assembly code in-line with C#
- Next by Date: I am puzzled by static class use
- Previous by thread: Re: C# inheritance broken?
- Next by thread: Re: C# inheritance broken?
- Index(es):
Relevant Pages
|