Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

casting question

Status
Not open for further replies.

kristolcrawley

Programmer
Jun 17, 2002
19
US
Can I cast one object to another if the methods contained in both match? I have one object called orderparts and another object which has the same properties as orderparts but it's called orderpartshold. I'm trying to save orderpartshold from orderparts without having to set each field individually. It keeps giving a compiler error, invalid cast.

Object obj = pOrderPartsTable;
OrderPartsHoldTable myOrderPartsHoldTable = (OrderPartsHoldTable ) obj;
 
You can only cast if one is a superclass of the other (or if one is a subclass of the other, and has been upcasted previously), or if one is an interface implemented by the other (or if both are classes that implement the same interface- they can both be casted as that interface, but not as each other). Based on how your classes are arranged, you might want to consider either making one a superclass of the other, or having them both implement the same interface.<br><br>if you're looking for a somewhat indepth list of different conversions, look here:<br><A HREF=" TARGET="_new" Title=" <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top