Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi people, I've ran into a problem where i wanna downcast a object of a different type to a object longer down in a derived hierarchy, i think the best way is just to show some simple code:

C#
class mammal {}
class dog : mammal{}
class cat : : mammal{}
class program
{
   public main()
   {
      mamal m = new mamal();
      cat c = new cat();
      c = (cat)m; //this give me an invalid cast exception!
      c = (m as cat); //same exception as above
   }
}


as seen i wanna give a cat object a mammal object/have all the info in the mammal putted into the cat object, which is derived from mammal, i tried implicit operators but saw that this is invalid to do in derived classes. so is there a way to do this without doing everything manually?

thanks - Jackie
Posted
Updated 16-Nov-12 12:07pm
v2

You can't cast an object to a type it is not. If it belongs to a different namespace then it is not the same class. You will have to create a converter:

C#
public static Namespace1.SomeClass Convert(Namespace2.SomeClass someClass) {
    Namespace1.SomeClass rtn = new Namespace1.SomeClass();
    rtn.SomeProp = someClass.SomeProp;
    rtn.SomeOtherProp = someClass.SomeOtherProp;
    return rtn;
}

or

You can use reflection, or make one derive from another and have one with common properties and other extend that


Also, if you own the code to one of the classes, you can check into overloading explicit and implicit on your class.

Good luck,
OI
 
Share this answer
 
Comments
Jackie00100 16-Nov-12 19:07pm    
both classes is in the same namespace above code was just for "show" the base type/super type is mammal and cat is derived from that. so i wanna put the base type into a derived class.

Thanks anyway
I don't think that make any sense downcasting an object, just assume cat has an extra property called CapacityToDrinkMilk which is integer that tells how much milk a cat can drink. What do u expect while downcasting in that property? which leads an object with wrong properties. You can create a method to convert mammal to cat where you can copy call the properties and add default values for the other properties. something like below

public static T ConvertMammalToSpecific<t>(this mammal mammalobject) where T : mammal, new()
{
T obj = new T();
//set all properties of mammal to new object
//set other properties in switch case/ dont do anything depends on your neeed
}

call this method to convert mammal to cat like below
mammal m = new mammal();
cat c = m.ConvertMammalToSpecific<cat>();

thanks
 
Share this answer
 
Comments
Jackie00100 16-Nov-12 18:48pm    
I see so i do need to convert everything manually? the whole point of a downcast is that i'm making some sort of game where you can make your own additions and some items are by default set to a class called "Item" (to save space and memory) but in some cases you want to transform this item into a "dark-relic" (a hexed/cursed item) but you'll never know what item it is or when it happens and therefor i needed a way to convert it, i do think I've found a solution but it only seems to work 50% of the time, never the least, thanks for your answer :)
Rohit Shrivastava 20-Nov-12 17:56pm    
from your comment it looks like you do not want to down cast rather you want to maintain state. I assume state pattern will help you... following is an implementation based on state pattern. please refer http://www.dofactory.com/Patterns/PatternState.aspx


public abstract class State
{
public abstract void Run();
}
public class CursedState : State
{
public override void Run()
{
Console.WriteLine("hahaha I am cursed;I will run slow");
}
}

public class BlessedState : State
{
public override void Run()
{
Console.WriteLine("hahaha I am blessed;I will run fast");
}
}
public class NormalState : State
{
public override void Run()
{
Console.WriteLine("I am neither blessed nor cursed;I will run at normal speed");
}
}

public class mammel
{
public State State { get; set; }
public void ChangeState(string state)
{
switch (state)
{
case "blessed":
State = new BlessedState();
break;
case "cursed":
State = new CursedState();
break;
default:
if (!(State is NormalState))
State = new NormalState();
break;
}
}
public void Run()
{
State.Run();
}
}
public class Cat : mammel
{
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900