473,320 Members | 1,821 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,320 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 1287
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 : ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.