Invalid cast from base to derived

  • Thread starter Thread starter INeedADip
  • Start date Start date
I

INeedADip

Any help would be greatly appreciated, here is an example:

public class Order{
...
}

public class OrderItems{
...
}

public class OrderDetails : Order {
private List<OrderItems> _items = new List<OrderItems>;

public static OrderDetails GetOrderDetails(int orderID){
OrderDetails order = (OrderDetails)DAL.GetOrder(orderID);
order.OrderItems = DAL.GetOrderItemsForOrder(orderID);
return order;
}
}

Hopefully you understand what I'm trying to do here. I have this
OrderDetails class that encompasses the Order and other properties,
but I get an error when trying to convert an "Order" to an
"OrderDetails". Can this be done? OrderDetails does inherit from
"Order".

Any pointers would be greatly appreciated.
 
Hi,

INeedADip said:
Any help would be greatly appreciated, here is an example:

public class Order{
...
}

public class OrderItems{
...
}

public class OrderDetails : Order {
private List<OrderItems> _items = new List<OrderItems>;

public static OrderDetails GetOrderDetails(int orderID){
OrderDetails order = (OrderDetails)DAL.GetOrder(orderID);
order.OrderItems = DAL.GetOrderItemsForOrder(orderID);
return order;
}
}

Hopefully you understand what I'm trying to do here.

I do, but I do like the way you are doing it, OrderDetail should not inherit
from Order, they are two different concepts. Order should contain a list of
OrderDetails instead.

Why are you getting the error though is not clear, you do not show the code
of DAL.GetOrder , but must sure it returns an Order isntance, not an
OrderDetail. That is why you are getting error.

I have this
 
INeedADip said:
Any help would be greatly appreciated, here is an example:

public class Order{
...
}

public class OrderItems{
...
}

public class OrderDetails : Order {
private List<OrderItems> _items = new List<OrderItems>;

public static OrderDetails GetOrderDetails(int orderID){
OrderDetails order = (OrderDetails)DAL.GetOrder(orderID);
order.OrderItems = DAL.GetOrderItemsForOrder(orderID);
return order;
}
}

Hopefully you understand what I'm trying to do here. I have this
OrderDetails class that encompasses the Order and other properties,
but I get an error when trying to convert an "Order" to an
"OrderDetails". Can this be done? OrderDetails does inherit from
"Order".

Cast is not the same as convert. A cast only works if the object instance,
*as originally created*, is really the derived type. To convert you need to
create a new derived instance and copy over the base fields.
 
Thanks for helping me here.

Order, OrderItem, AdditionalCharge, and other objects of this nature
are 1 to 1 mappings to tables in a database that I can't really
change. Instead of passing around and order and a collection of
OrderItem and AdditionCharge (etc...) I figured I would create an
object that encompasses all of them called OrderDetails.

DAL.GetOrder does return an object of type "Order" but on that line I
get:

"Unable to cast object of type 'Order' to type 'OrderDetails'"
 
OK, my fault....I guess I want to cast it.

What do you mean by "as originally created"?
Does this mean that I have to copy all the base fields by hand? Then
every change to Order I have to re-code OrderDetails?

Sorry for the stupid questions..
 
Is this an instance where I should maby use the Duck Typing I've read
about, or am I missing some basic OO principals?
 
Well, to answer my own question(s)...I think I am missing some basic
OO principals. I think I'm asking to break the underlying object
model by saying that an Order is an OrderDetails.

I've just created a new constructor on OrderDetails that takes an
objec tof type Order or orderID, then gets the appropriate order and
fills all the properties...Without confirmation this probably won't
help anyone else because I'm no expert (obviously).
 
INeedADip said:
[...]
I've just created a new constructor on OrderDetails that takes an
objec tof type Order or orderID, then gets the appropriate order and
fills all the properties...Without confirmation this probably won't
help anyone else because I'm no expert (obviously).

Assuming you have also fixed the OrderDetails class so that it does not
actually derive from Order, yes...that would be a nice solution.

Pete
 
INeedADip said:
Well, to answer my own question(s)...I think I am missing some basic
OO principals. I think I'm asking to break the underlying object
model by saying that an Order is an OrderDetails.

I've just created a new constructor on OrderDetails that takes an
objec tof type Order or orderID, then gets the appropriate order and
fills all the properties...Without confirmation this probably won't
help anyone else because I'm no expert (obviously).

Sounds like the right solution, no "fix" necessary.
 
INeedADip said:
OK, my fault....I guess I want to cast it.

What do you mean by "as originally created"?

When you did "new Order()", .NET made enough space for an Order object (and
also set up the virtual table for an Order object, you couldn't cast the way
you wanted even if OrderDetails was the same size).
Does this mean that I have to copy all the base fields by hand? Then
every change to Order I have to re-code OrderDetails?

Well, you could put the associated code inside Order, either as a
constructor or a static member function in the pattern of Array.Copy.
Sorry for the stupid questions..

Seen stupid questions, yours aren't.
 
Hi,

INeedADip said:
Thanks for helping me here.

Order, OrderItem, AdditionalCharge, and other objects of this nature
are 1 to 1 mappings to tables in a database that I can't really
change.

Ok, that is fine.

Instead of passing around and order and a collection of
OrderItem and AdditionCharge (etc...) I figured I would create an
object that encompasses all of them called OrderDetails.

There is the error, an Order is not an OrderDetail, why you want to treat
them in the same way?
For example an OrderDetail cannot exist outside an Order.
DAL.GetOrder does return an object of type "Order" but on that line I
get:

"Unable to cast object of type 'Order' to type 'OrderDetails'"

Cause they are sibling, you cannot cast one into the other.
 
Back
Top