public class SuperCar: Car
{
public bool SuperWheels { get {return true; } }
}
public class Car
{
public bool HasSteeringWheel { get {return true;} }
}
How can I set the base class for the derived Supercar?
For example, I want to simply set SuperCars base class like this:
public void SetCar( Car car )
{
SuperCar scar = new SuperCar();
car.Base = car;
}
Basically, if I have Car objects, I do not want to manually iterate through every property of the car in order to setup the SuperCar oject, which I think is the only way you can do it but if you can do it the other way it would be sooo much better.
No, that's not possible since assigning it to a derived class reference would be like saying "Base class is a fully capable substitute for derived class, it can do everything the derived class can do", which is not true since derived classes in general offer more functionality than their base class (at least, that's ...
A derived class can have only one direct base class.
Suppose, the same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class, the function of the derived class is executed. This is known as function overriding in C++. The function in derived class overrides the function in base class.
A base class is an existing class from which the other classes are derived and inherit the methods and properties. A derived class is a class that is constructed from a base class or an existing class. 2. Base class can't acquire the methods and properties of the derived class.
I use something like this in the subclass and it works fine for me:
using System.Reflection;
.
.
.
/// <summary> copy base class instance's property values to this object. </summary>
private void InitInhertedProperties (object baseClassInstance)
{
foreach (PropertyInfo propertyInfo in baseClassInstance.GetType().GetProperties())
{
object value = propertyInfo.GetValue(baseClassInstance, null);
if (null != value) propertyInfo.SetValue(this, value, null);
}
}
If I'm understanding your question correctly (and I'm not entirely sure that I am), then you can sort of get the behavior you want by doing something like this:
class Car {
public bool CarProperty { get; set; }
// regular constructor
public Car() {
}
// "copy" constructor
public Car(Car c) {
CarProperty = c.CarProperty;
}
}
class SuperCar : Car {
public bool SuperCarProperty { get; set; }
// regular constructor
public SuperCar() {
}
// "copy" constructor
public SuperCar(Car c) : base(c) {
SuperCar sc = c as SuperCar;
if(sc != null) {
SuperCarProperty = sc.SuperCarProperty;
}
}
Then you can do this:
public void SetCar(Car car) {
SuperCar scar = new SuperCar(car);
}
Note that you have to be very careful in your "copy" constructor not to copy properties in such a way that two objects share the same members (references) when they should not.
I have to ask, though, what your goal is with this?
This is not possible.
You need to manually set the properties.
You can only copy the contents of another class, but not set it directly. See example below using what is called a copy constructor.
class Car
{
string model;
public Car(string model) { this.model = model; }
protected Car(Car other) { this.model = other.model; }
string Model { get; set; }
}
class SuperCar : Car
{
public SuperCar(Car car) : base(car) { }
public SuperCar(string model) : base(model) { }
bool IsTurbo { get; set; }
}
The key is the keyword base()
after the constructor declaration.
Overall a lot of helpful comments. I think the short answer was given by pst and I think this is correct:
No. Short reason: There is no separate base object. (object)this == (object)base is always true. There are ways to perform cloning/copying by reflection (and other means) though. Perhaps describe what is really wanted
So, his suggestion of using the automapper tool was also incredibly useful and was basically what I was looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With