Programming Tools
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingProgramming Tools

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old July 18th, 2003, 07:10 PM
si_c si_c is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 2 si_c User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old July 19th, 2003, 10:27 AM
markerdave markerdave is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Illinois, USA
Posts: 71 markerdave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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...

Reply With Quote
  #3  
Old July 19th, 2003, 10:50 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Quote:
Originally posted by markerdave
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.


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

Reply With Quote
  #4  
Old July 21st, 2003, 01:25 PM
si_c si_c is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 2 si_c User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingProgramming Tools > Casting Between Classes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway