473,373 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

Casting in and out

Is it possible to force a cast to an inherited class?

class Program
{
static void Main(string [] args)
{
Father Dad = new Father( );
Son boy = (Son) Dad; // <<== fails at run time with
invalid cast
}
class Father { }
class Son : Father { }
}

Is there any way to allow the son to follow in his father's footsteps...

--
Paul
Jan 11 '06 #1
5 1288
public class Father
{
string m_Name;

public string Name
{
get
{
return this.m_Name;
}

set
{

this.m_Name = value;
}

}
}

public class Son : Father

{

....

}
Paulustrious wrote:
Is it possible to force a cast to an inherited class?

class Program
{
static void Main(string [] args)
{
Father Dad = new Father( );
Son boy = (Son) Dad; // <<== fails at run time with
invalid cast
}
class Father { }
class Son : Father { }
}

Is there any way to allow the son to follow in his father's footsteps...

--
Paul


Jan 11 '06 #2
Paulustrious wrote:
Is it possible to force a cast to an inherited class?


Unless one of the classes involved explicitly knows how to do it (eg
Father has a ToSon method, or Son has a constructor taking a Father)
then no. Even then, it would be a case of building a new object. You
can't tell the system that something of one type is actually of a more
derived type.

Can you give a more concrete example of why you want to do this? There
may well be a different idiom which would be useful for you.

Jon

Jan 11 '06 #3
Paulustrious wrote:
Is it possible to force a cast to an inherited class?

Is there any way to allow the son to follow in his father's
footsteps...


Do you need the object cast as a different object? Or can you simply use
properties?

// Using VS2005.
class Person
{
private string m_Name = String.Empty;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
}

class PersonCollection : List<Person> {}

class FamilyMember : Person
{
private Person m_Father = new Person();
public Person Father
{
get { return m_Father; }
set { m_Father = value; }
}

private PersonCollection m_Sons = new PersonCollection();
public PersonCollection Sons
{
get { return m_Sons; }
set { m_Sons = value; }
}
}

--
John T
http://sage1solutions.com/TknoFlyer
Reduce spam. Use Sender Policy Framework: http://spf.pobox.com
____________________
Jan 11 '06 #4
Thanks you for your reply. What I hadn't realised was the runtime checking of
casting operations. I was used to 'unsafe' casting wher it was down to the
coder to ensure type-safe operations. It highlighted a structural error in
what I was coding. I understood once I realised that the following worked.

class Program
{
static void Main(string [] args)
{
Egg egg = new Egg ( );
Chicken chicken = egg;
egg = (Egg)chicken;
}
class Chicken { }
class Egg : Chicken { }
}

Thanks for your help..
(To quote Arnold ... I'll be back)
--
Paul

Jan 11 '06 #5

How very strange - I was writing a template collection class and you seem to
have foreseen that. It was a normal tree but with some specilized additions.

As I said earlier / elsewhere I was structuring my creations wrongly.

class FamilyMember<T> where T : new()
{
FamilyMember<T> Parent.....get set
FamilyMember<T> [] Children... get set
T Data .... get set

public AddChild( T pData) ...and so on
}
class Food {}
class FoodMember : FamilyMember<T>
{}

I was adding Children in the form of FamilyMember<T> and trying to retrieve
them as a FoodMember...or something like that.

Sorry to have wasted people's time

Regards - Paul whoisat PaulCotter.com

Jan 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.