Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend object with values from parent

Let's say I've got a class:

public class Parent
{
    public string Name { get; set; }
    public string City { get; set; }
}

and in some function I'm getting the list of objects type Parent, next I'd like to extend those objects with new field with some value, so I'm declaring an extended class like this:

public class Child : Parent
{
    public Child(Parent parent)
    {
        Name = parent.Name;
        City = parent.City;
    }
    public int Age { get; set; }
}

and call the costructor for each extended object. Is there a better way to do that? What if there will be multiple properties in Parent? Maybe there is some more elegant way to achieve that?

like image 444
Steve Macculan Avatar asked May 06 '26 08:05

Steve Macculan


1 Answers

I think maybe you're looking for a copy-constructor pattern. Each level defines a protected constructor which copies the relevant properties:

public class Parent
{
    public string Name { get; set; }
    public string City { get; set; }

    //normal constructor
    public Parent()
    {

    }

    protected Parent(Parent copy)
    {
        this.Name = copy.Name;
        this.City = copy.City;
    }
}

The Child would inherit from Parent, pass it down through to the copy-constructor, then append its new values as desired:

public class Child : Parent
{
    public string NewInfo { get; set; }

    public Child(Parent copy)
        : base(copy)
    {

    }
}

Usage might look like:

Parent parent = new Parent() { Name = "Name", City = "StackOverflow"};

Child child = new Child(parent) { NewInfo = "Something new!" };

Console.WriteLine(child.Name); //Name
Console.WriteLine(child.City); //StackOverflow
Console.WriteLine(child.NewInfo); //Something new!

The benefit from this is that you can have multiple levels of inheritance with each level managing their own properties.

EDIT: Given your most recent comment:

The motivation to this question is a situation in where I'm getting a list of objects with data, and want to show this data but with some additional fields, without touching the base class.

Perhaps the better method then is to wrap the base class:

public class Child
{
    private readonly Parent WrappedParent;

    public string NewInfo { get; set; }

    public string Name 
    { 
        get { return WrappedParent.Name; }
        set { WrappedParent.Name = value; }
    }

    public string City 
    { 
        get { return WrappedParent.City; }
        set { WrappedParent.City = value; }
    }

    public Child(Parent wrappedParent)
    {
        this.WrappedParent = wrappedParent; 
    }
}

Downside is you have to redeclare each property, and you are no longer inheriting (cannot be considered a) "Parent", but then you are definitly "not touching" the base class anymore. Could move the "Parent" properties into an IParent interface if that's better for you, but doing so again is "touching" the base class as you'll have to add the IParent interface declaration to its class definition.

like image 165
Chris Sinclair Avatar answered May 09 '26 03:05

Chris Sinclair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!