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

21,175

Solution 1

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.

Solution 2

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);
    }
}

Solution 3

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?

Solution 4

This is not possible.

You need to manually set the properties.

Solution 5

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.

Share:
21,175
Watson
Author by

Watson

I'm like James Woods.

Updated on July 13, 2022

Comments

  • Watson
    Watson almost 2 years
    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.

  • Watson
    Watson over 13 years
    My goal is simple. Basically, in real life Supercar would probably have 100's of properties. So lets say I now maintain a database of supercars. Now, I want to inherit from supercars but I want to pass the base class of the supercars I have already created for sheer conveniance. I dont want to manually set 100 properties when I HAVE ALREADY created the base type.
  • Watson
    Watson over 13 years
    Lets say I have a person class, and of course there are thousands of data items that represent the person class. The person class defines ten thousand properties. Now, I have a person id that associates to particular person class I want to derive from and create a "John" class inheritated from person. Now I know a particular person ID I want to pass in and that will be the base of "John".
  • SLaks
    SLaks over 13 years
    You can make a wrapper instead of using inheritance.
  • siride
    siride over 13 years
    Sounds like you need to break up SuperCar into smaller objects. Then objects that are the same between SuperCars can be shared.
  • Watson
    Watson over 13 years
    I'm more concerned with my understand of inheritance in c#. It just seems to me, that if class A derives from class B, you should be able to set B on A. So if you have an instance of an A class you should be able to say A.B = new B();
  • Watson
    Watson over 13 years
    In my dream world you can add to class A like this to expose base B: public B GetSetBase { get { return base; set { base = value;} }
  • siride
    siride over 13 years
    I don't think you understand inheritance very well. Inheritance is about defining forms for a set of objects, not specific details for a specific object. For the latter, you want to use composition.
  • Watson
    Watson over 13 years
    Yet in this case, I have both. I have a form for a set of objects and I also have the specific details. What does that file under?
  • siride
    siride over 13 years
    @user190084: it files under using both inheritance and composition. The fact is, you can't set the base class at runtime and to be perfectly honest, it generally doesn't make sense unless you have a language that uses duck-typing (and even then, it's real dangerous).
  • Watson
    Watson over 13 years
    I will look into this further. I really appreciate the feedback. Thank you very much.
  • Watson
    Watson over 10 years
    Yes. This is was perfect.
  • Michael Sefranek
    Michael Sefranek over 6 years
    that is interesting, but it doesn't really address the issue of iterating through 30 properties unless I am totally missing something.
  • John Alexiou
    John Alexiou over 6 years
    If manual setting of properties is desired, then this answer explains it better on how to best use copy constructors.