Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derived and base class, can I set the base explicitly?

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.

like image 855
Watson Avatar asked Jan 28 '11 01:01

Watson


People also ask

Can we assign base class to derived class?

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 ...

Can a class be both a base class and a derived class?

A derived class can have only one direct base class.

Can base and derived class have same function?

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.

How base class and derived class related to each other?

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.


5 Answers

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);
    }
}
like image 56
vyengr Avatar answered Sep 30 '22 15:09

vyengr


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?

like image 35
siride Avatar answered Sep 30 '22 16:09

siride


This is not possible.

You need to manually set the properties.

like image 35
SLaks Avatar answered Sep 30 '22 16:09

SLaks


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.

like image 29
John Alexiou Avatar answered Sep 30 '22 16:09

John Alexiou


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.

like image 33
Watson Avatar answered Sep 30 '22 16:09

Watson