2

I have a custom class

class MyClassA{
    public int Width { get; set; } 
}

and another custom class B

class MyClassB {
    public MyClassA paramClassA;
    public int Height { get; set; }
}

Is there a way to have a constructor inside MyClassB which accepts a parameters of type MyClassB and automatically assigns values to the attribute?

Something like this:

class MyClassB{
    public MyClassA paramClassA;
    public int Height { get; set; }

    public MyClassB(MyClassB param){

    }

    public MyClassB(MyClassB param){
        // automatically assign properties of param to the instance it is created
    }
}

So I can do this:

var classB = new MyClassB();
classB.Height = 100;
classB.paramClassA = new MyClassA();
classB.paramClassA.Width = 100;

var classB2 = new MyClassB(classB);

Is there a way to do this?

giani.sim
  • 257
  • 2
  • 16

3 Answers3

2

There´s no in-built way to do this. Anyway this is the purpose of a copy-constructor and can be achieved by copying all the properties from your existing instance to the current one. However when you have a deep inheritance-chain copying all those members can be non-trivial task which also includes private members.

class MyClassB{
    public MyClassA paramClassA;
    public int Height { get; set; }

    public MyClassB(MyClassB param){
        this.Heigth = param.Height;
        this.MyClassA = param.MyClassA;
    }
}

You could also implement ICloneable:

class MyClassB : ICloneable {
    public MyClassA paramClassA;
    public int Height { get; set; }

    public object Clone(){
        return new MyClassB 
        {
            this.Height;
            this.MyClassA;
        };
    }
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2

Yes, you can do it manually. Remember about differences between shallow copy and deep copy in C# language:

class MyClassB{
    public MyClassA paramClassA;
    public int Height { get; set; }

    public MyClassB(MyClassB param){
        Height = param.Height;
        paramClassA = new MyClassA();
        if (param.paramClassA != null)
        {
          paramClassA.Width = param.paramClassA.Width;
        }
    }
}

I'll recommend Clone method inside MyClassB class:

class MyClassB{
    //....

    public MyCLassB Clone()
    {
      var result = new MyClassB
      {
        Height = Height
      };
      result.paramClassA = new MyClassA();
      if (paramClassA != null)
      {
        result.paramClassA.Width = paramClassA.Width;
      }
    }
}

And use it, like below:

var classB = new MyClassB();
classB.Height = 100;
classB.paramClassA = new MyClassA();
classB.paramClassA.Width = 100;

var classB2 = classB.Clone();
Community
  • 1
  • 1
Pawel Maga
  • 5,428
  • 3
  • 38
  • 62
0

You could always create a copy method that would assign the values and return a MyClassB where you assign the values within. If you must do it in a constructor check out the following.

public MyClassB(MyClassB param)
{
    Width = param.Width;
    // If you want to keep the same reference classA
    paramClassA = param.paramClassA;
    // if you want the classA to not be the same reference you could always do.
    paramClassA = new MyClassA() { Width =  param.Width };
}
Chad Wentz
  • 16
  • 1