giani.sim
giani.sim

Reputation: 277

C# constructor that accepts parameter of the same class

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?

Upvotes: 2

Views: 1484

Answers (3)

Chad Wentz
Chad Wentz

Reputation: 16

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

Upvotes: 0

Pawel Maga
Pawel Maga

Reputation: 5787

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

Upvotes: 2

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

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

Upvotes: 2

Related Questions