4

I am looking to get a child class instantiated using a parent instance to set all of the inherited variables.

For example:

public class Foo
{
    //Variables
}

public class bar : Foo
{
    public int ID { get; set; }

    public bar(Foo instance)
    {
        base = instance; // Doesn't work but is generally the idea I'm looking for
    }
}
Qinusty
  • 329
  • 2
  • 3
  • 8

4 Answers4

1

Just add a constructor to your parent Foo class that accepts a Foo instance and takes care of copying the fields. The Bar class can then invoke that as the base constructor.

public class Foo
{
    private string var1;
    private string var2;

    public Foo() { }

    public Foo(Foo otherFoo)
    {
        this.var1 = otherFoo.var1;
        this.var2 = otherFoo.var2;
    }
}

public class Bar : Foo
{
    public int ID { get; set; }

    public Bar(Foo instance)
        : base(instance)
    {
    }
}
sstan
  • 35,425
  • 6
  • 48
  • 66
0

You would have to make all your members public inside Foo that would need to be set from the constructor. Then, set the properties you need to.

Bigsby
  • 952
  • 6
  • 20
0

Why can't you get it done through a public auto property like below. BTW, what you are trying to have is Composition wherein bar object is composed of foo object. Not sure why you are still inheriting from foo then?

public class bar : Foo
{
    public int ID { get; set; }
    public Foo baseinstance { get; set;} 

    public bar(Foo instance)
    {
        baseinstance = instance; 
    }
}

I am not absolutely sure but from your posted code (specifically the below part) it feels like you are trying to call the base class constructor or constructor initializer since you said

I am looking to get a child class instantiated using a parent instance to set all of the inherited variables.

public bar(Foo instance)
{
    base = instance; 
}

Which should actually be

public bar(Foo instance) : base()
{
   //child instance initialization
}

But that's not needed for a parameterless constructor cause when you instantiate child object constructor initializer will get invoked and base class will gets initialize first in-order to have the base member initialized before child initialization.

If you have parameterized constructor in base and in child then you can explicitly call base constructor

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • I'm using the class as a entity for use with EF6 and afaik EF uses the properties within a class to form a table. A baseinstance would create a seperate table linked by primary and foreign keys. – Qinusty Aug 15 '15 at 23:03
0

I think you are looking for some kind of mapper that maps from one object to another.

You could use a open source project like AutoMapper or ValueInjecter which can be found on NuGet.

Here is also a nice article: Copy values from one object to another

Demo with ValueInjecter:

static void Main(string[] args)
{
    var foo = new Foo()
    {
        Property1 = "Value1",
        Property2 = "Value2"
    };

    var bar = Mapper.Map<Bar>(foo);
    bar.Id = 3;

    Console.WriteLine(bar.Id); //3
    Console.WriteLine(bar.Property1); //Value1
    Console.WriteLine(bar.Property2); //Value2
}
Community
  • 1
  • 1
Jakob
  • 536
  • 4
  • 16