viperjason

I expected this code to go more simply but it doesnt.

I want to cast the parent to the child class as the following:

class parent
{
public void doStuff(){}
}
class child : parent
{
public void domorestuff() {}
}

parent p = new parent();
child c = (child)p;

How can I make that work



Re: Visual C# Language Casting parent class to child

OmegaMan

You can't downcast if that is right term. If the parent object is created, it cannot be cast to the child...but here is a way to work around it...

Create an interface which the parent implements. Have the child override functionality if needed or just expose the parents functionality. Change the cast to be an interface pointer and do the operations.





Re: Visual C# Language Casting parent class to child

viperjason

Got a better answer that doesn't require me to change tons of my code.




Re: Visual C# Language Casting parent class to child

OmegaMan

viperjason wrote:
Got a better answer that doesn't require me to change tons of my code.


Please share.





Re: Visual C# Language Casting parent class to child

viperjason

Sorry that was a question not a statement. I was asking if you had a better answer that wouldnt require me to change a lot of code.




Re: Visual C# Language Casting parent class to child

OmegaMan

What is the why behind the need for casting to the child from a parent That may yield a different answer.





Re: Visual C# Language Casting parent class to child

Kane-

You can take a look at this: http://weblogs.sqlteam.com/mladenp/archive/2006/06/30/10430.aspx

You will need to implement the IConvertable interface on both of your classes.

I would highly recommend you take a look at your general design though as this is not an ideal way of doing things. You should consider using Interfaces, generics and possibly even reflection if you've have a big enough need to justify it.




Re: Visual C# Language Casting parent class to child

viperjason

Ok, sure why not. Let me explain it further.
The main reason is design time coding conventions. Using only the class that is required to get the job done.

I have a class that is a "description" of an SGML file (no XML parsing and such doesnt work)

So for every possible tag in the SGML file I have a public property for that tag. The class name is the type of SGML file. I have a function that takes ANY type and parses an SGML file according to the type definition (of course this uses reflection and custom attributes). Now I could send the type of the child class to get back a child class, but that would be sending the wrong class. Even though my class derives off of the SGML class, it would (coding convention wise) be the wrong class to send.

I've decided I'm just going to send it the child class so I can get the child class back, it is just disappointing that I cant send the parent instead as that would look better when the code is reviewed. And doing this in reflection would just take too long.

Oh well, I just hate to settle for bad looking code.




Re: Visual C# Language Casting parent class to child

Robert C. Barth

You can't make it work. Think about what you're asking.

In the code, by inherting from parent, you're saying "Child is a kind of Parent". Okay, sounds weird due to the names, but perfectly acceptable. Then with the "child c = (child)p" cast, you are, in essence, telling the compiler that "Parent is a kind of Child". It's not. The inference is backwards.

As previously stated, I would re-work the class diagram if you need something like this to work. It sounds like some sort of mistake was made in the inheritance architecture somewhere. If all you need is for both child and parent to implement common methods so that when either of the objects is passed to some function it can operate on some common methods on the passed-in object (holy polymorphism, Batman!), then just have the two of them implement the same interface, or type the function parameter as the base class and make sure the methods exist on the base class.





Re: Visual C# Language Casting parent class to child

viperjason

Actually what I expect the compiler to do is to allocate the memory for the child, copy the memory from parent to child and be done with it.

Something like this was so easy in C (And before you say that classes didnt exist in C....they did you just had to do more of the work....mainly using pointers of functions and structures).

In my case, parent and child have 0 (yes 0) methods and are basically structures. But to make things look nice, and to add functions later classes were used. I dont need them to use common methods(I know of polymorphism) , I need the common data that is between them.

Either way, I am using my workaround as stated above. Its not what I wanted but it works.




Re: Visual C# Language Casting parent class to child

OmegaMan

viperjason wrote:
Actually what I expect the compiler to do is to allocate the memory for the child, copy the memory from parent to child and be done with it.


Ahhh yes, the union operation in C.

But the operation, from what I understand, sounds like a factory architecture. information is pulled from disparate sources and an object is returned....I am not seeing why this is ugly in a review...

This isn't a quick fix...and maybe a shot in the dark but, maybe instead of a workable class, maybe what should be returned is a data vector object that is usable among both the parent and derived classes.

That way regardless of what is created, it can take the vector and operate on it. Hence satisfying the requirement of shared data.







Re: Visual C# Language Casting parent class to child

Mark Dawson

Hi,

> Actually what I expect the compiler to do is to allocate the memory for the child, copy the memory from parent to child and be done with it.

You could create a constructor in the Child that accepts a Parent instance and then copies all of the data across. It would not be casting, but it sounds like what you mentioned above, would that be acceptable. I would recommend refactoring your code so that you would not need to do this i.e.

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication10

{

class Program

{

static void Main(string[] args)

{

Parent p = new Parent("Bob");

Child c = new Child(p, "Drum");

}

}

class Parent

{

string name;

public Parent(string name)

{

this.name = name;

}

public string Name

{

get

{

return this.name;

}

}

}

class Child : Parent

{

string favouriteToy;

public Child(Parent parent, string favouriteToy) : base(parent.Name)

{

this.favouriteToy = "None";

}

}

}

Mark.






Re: Visual C# Language Casting parent class to child

viperjason

OmegaMan wrote:
Ahhh yes, the union operation in C.

But the operation, from what I understand, sounds like a factory architecture. information is pulled from disparate sources and an object is returned....I am not seeing why this is ugly in a review...

This isn't a quick fix...and maybe a shot in the dark but, maybe instead of a workable class, maybe what should be returned is a data vector object that is usable among both the parent and derived classes.

That way regardless of what is created, it can take the vector and operate on it. Hence satisfying the requirement of shared data.

Not quite the union operation, but basically the same idea.

Yes, what I am doing is a factory architecture where you give it a SGML file and the corresponding SGML object and it returns an array of objects of the type given.

Just it seems that without the casting, I need to send it the child and the only part that will be used in the factory function will be the parent. I guess that works.

Thanks again for all of your help.