|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Casting Between Classes
Hi All
I'm trying to get my head around casting between classes. I can cast from a class to it base easily enough, but not the other way around (which is of course the way I want to go )For example: Code:
public class Primary
{
protected Primary()
{
//
// TODO: Add constructor logic here
//
}
public void test()
{
System.Web.HttpContext.Current.Response.Write("test");
}
}
//Uses Primary as base
public class Second : Primary
{
public Second()
{
}
}
The following code then works fine: Code:
Second sc = new Second(); Primary pc = (Primary) sc; But this doesn't : Code:
Primary p2 = new Primary(); Second s2 = (Second) p2; Why???? Any thoughts? Cheers Si |
|
#2
|
|||
|
|||
|
I am not sure that you can cast both ways. I know in Java that you can cast up in the class hierarchy, but not down. That is because with your example a Second object is guaranteed to have all the functionality of a Primary object since it inherited from it. But the opposite isn't true since the child class could have added more methods. Even if you don't add more methods, I don't think it is allowed. Basically as a general rule you should be able to cast up the class hierarchy, but you probably won't be able to cast down it. Hope that helps.
__________________
Oh I wish, I wish I hadn't killed that fish... |
|
#3
|
|||
|
|||
|
Quote:
Yeah, if I remember correctly, Markerdave is right... It's always the little, albeit important, issues that I seem to forget! But he's right... You can cast upwards in the hierarchy, as the child (Secondary) inherits all the methods and attributes of its parent class (Primary). However, a parent class cannot inherit methods/properties from its child... Think of it in real-world terms... You inherit certain characteristics from your parents (magic of DNA! ), but you parents cannot inherit anything from you... It's a one-way passage.I hope this helps... If I am wrong, however, please correct me. ![]()
__________________
____________________________________________ Developer Shed Weekly Writer | DevArticles Forum Moderator Build Your Own KlipFolio Klip With PHP FrankManno.com - Under Construction Design Interactive Group - Under Construction |
|
#4
|
|||
|
|||
|
Hmm....
Problem is I know that there must be a way to allow you to cast from the parent to the inheritor. In Microsoft Content Management Server, the Search functions return a class called ChannelItem. The Posting and Channel objects both inherit from the ChannelItem class. When you get a return from the search function, you must cast to the Channel or Posting item. For example: Code:
Posting myPosting = (Posting) Searches.GetByGuid("aGuidString");
Also this works with Placeholders where you must cast the return from a placeholder collection as the appropriate type of placeholder. Each placeholder inherits from the same returned BasePlaceholder class. What am I missing here? ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Programming Tools > Casting Between Classes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|