Inheritance base... AGAIN!!!

  • Thread starter Thread starter news
  • Start date Start date
N

news

I need ... maybe this construction possible????

class OrderHeader
{
....
// method, that passing initialized base class (OrderHeader) into
new Order
public Order ConvertToOrder()
{
return (Order) this;
}

}

class Order : OrderHeader
{
....
}


Can I simply convert base class into inherited class???
Or how to pass initialized base class into new inherited_class ????

Thanks

Mike
 
Hi,

The line :

return (Order) this;

is not possible, it ALWAYS is going to give you error. the thing is that
Order can be used as OrderHeader , but the opposite ( which is what you are
doing ) is NEVER possible.

I think that your problem is more of design, maybe you need composition
instead of inheritance, what if you declare three classes Order ,
OrderHeader , OrderBody and Order has a member of type OrderHeader and
OrderBody ?

Cheers,
 
What do you really want to do by converting base class into inherited
class??
Maybe it's not the correct way...


____________________
Franky
(e-mail address removed)
 
Hi,

I suggest reading a little more about object-oriented programming.
You are violating a very fundamental rule of the OO paradigm.
You can never cast a pointer (or reference) to a class into a pointer (or
reference) to a subclass of that class.
The opposite is ok.

Greetings,

Bram.
 
Hi,
My guess is that you have a list of base classes and your are trying to see
if any of those is a certain derived type.

You have OrderHeader and you want to know if it is of Order type. Say your
code is

class OrderHeader {}

class Order : OrderHeader {}

class OtherOrder : OrderHeader {}

If that is the case you can write code like this

Order order = orderHearder as Order;
if (null != order)
{
// The orderHeader base instance class is actually a Order
instance cast to its base class.

order.MyMethodInOrderClass();
}
else if
{
OtherOrder otherOrder = orderHeader as OtherOrder;
if (null != otherOrder)
{
// The orderHeader base instance class is actually a
OtherOrder instnce cast to its base
otherOrder.MyMethodInOtherOrderClass();
}
}

Hope that helps

--------------------
From: "(e-mail address removed)" <[email protected]>
Subject: Inheritance base... AGAIN!!!
Date: Wed, 28 Jan 2004 15:20:59 +0100
Lines: 28
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework,microsoft.public.dotnet.l
anguages.csharp
NNTP-Posting-Host: gw.coty.cz 62.77.75.65
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.csharp:215995 microsoft.public.dotnet.framework.compactframework:44245
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I need ... maybe this construction possible????

class OrderHeader
{
...
// method, that passing initialized base class (OrderHeader) into
new Order
public Order ConvertToOrder()
{
return (Order) this;
}

}

class Order : OrderHeader
{
...
}


Can I simply convert base class into inherited class???
Or how to pass initialized base class into new inherited_class ????

Thanks

Mike


Rakesh, EFT.

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
Back
Top