Re: C# inheritance broken?
- From: "Michael D. Ober" <obermd.@.alum.mit.edu.no.spam>
- Date: Sat, 27 Jan 2007 05:04:55 GMT
"Bruce Wood" <brucewood@xxxxxxxxxx> wrote in message
news:1169856962.726497.102330@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
A generalized method of converting a base class into a derived class with
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
Ignacio Machin
machin AT laceupsolutions comThis isn't a C# issue. It is a dotNet
issue as I have run into the same
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.
the knowledge that the derived class may still require some initialization
would definitely be useful, even if you had to do it via a constructor that
takes an object of the base class as it's argument. The missing syntax, in
vb is
mybase = objBaseClass
In C# I think it would be
base = objBaseClass
In either case, since this is occurring in a constructor in place of the
base.New() or mybase.New statement, the programmer knows he still has to
instantiate and initialize all other private and protected objects in the
derived class.
Mike Ober.
.
- Follow-Ups:
- Re: C# inheritance broken?
- From: Bruce Wood
- 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
- Re: C# inheritance broken?
- From: Bruce Wood
- C# inheritance broken?
- Prev by Date: Re: C# inheritance broken?
- Next by Date: Re: Access public methods in multple copies of the same form
- Previous by thread: Re: C# inheritance broken?
- Next by thread: Re: C# inheritance broken?
- Index(es):
Relevant Pages
|