Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to cast parent class to child but not overwriting existing data in parent class ?

If I have this than data which is already in Base class instance will be overwritten with new Derived().

Base derivedInstance = new Derived();
Derived good = (Derived)derivedInstance;



Hope You understand my question ?

What I have tried:

Base derivedInstance = new Derived();
Derived good = (Derived)derivedInstance;
Posted
Updated 8-Feb-18 22:10pm

When you cast something, you don;t "throw away" anything from the object, it doesn't actually change the object in any way at all. All it does is change what you can do with it in terms of your code.
Think about cars for a moment.
You have a Car class:
C#
public class Car
   {
   public int CountWheels { get { return 4; } }
   }
And you use that to create a derived class for each manufacturer:
C#
public class Ford : Car
   {
   public bool MadeInGermany { get { return false;} }
   }
public class MercedesBenz : Car
   {
   public bool MadeInAmerica { get { return false;} }
   }
You can manipulate a Ford:
C#
Car thatCar = new Ford();
Ford thisCar = (Ford) thatCar;
Console.WriteLine("{0}:{1}", thisCar.CountWheels, thisCar.MadeInGermany);
And casting it doesn't do anything to it - it remains both a Car and a Ford.
What you can't do is cast a Mercedes to a Ford:
C#
Car thatCar = new MercedesBenz();
Ford thisCar = (Ford) thatCar;
Console.WriteLine("{0}:{1}", thisCar.CountWheels, thisCar.MadeInGermany);
And the system will thrown an exception, because a Ford and a Mercedes are not the same - you can't take a Mercedes badge, glue it to a ford and expect people to believe it! :laugh:

If you have an instance of the base class in derivedInstance before you do this:
derivedInstance = new Derived();
Then the system will discard the reference to that instance when you load the new value - but it won't change that instance. In your code
Base derivedInstance = new Derived();
derivedInstance is a new variable, so it contained no previous instance anyway, the line defining the variable sets it's first value.
 
Share this answer
 
You could use a different constructor in your Derived class, see example here:
How to: Initialize Objects by Using an Object Initializer (C# Programming Guide) | Microsoft Docs[^]
You might also be interested in ICloneable, see example here: [^]
But this does not work in all cases, so your serializing option might be better ...
 
Share this answer
 
v2
Comments
Member 13568484 9-Feb-18 4:03am    
Thank You. I will take a look a bit later today, as for now I found workaround :)
I found this solution:

var serializedParent = JsonConvert.SerializeObject(parentInstance); 
 Child c  = JsonConvert.DeserializeObject<Child>(serializedParent);


this will keep records from Parent class :)
 
Share this answer
 

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