471,317 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 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 1230
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jacob Jensen | last post: by
231 posts views Thread by Brian Blais | last post: by
35 posts views Thread by ytrama | last post: by
7 posts views Thread by Jim Bancroft | last post: by
7 posts views Thread by S. Lorétan | last post: by
17 posts views Thread by sophia.agnes | last post: by
9 posts views Thread by Taras_96 | last post: by
101 posts views Thread by Tinkertim | last post: by

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.