0

I'm having trouble with copying an object.

I want to copy object1 and then make changes to object2 without changing object1.

// I'm cloning lower.Rebar object here
upper.Rebar = lower.Rebar.Clone();

// When I make changes to upper.Rebar, lower.Rebar gets changed too.
upper.Rebar.Polyline.Points.RemoveAll(p => p.Y < breaklevel);

// Here is how I'm cloning the lower.Rebar object
public ColumnElevationRebar Clone()
{
    var obj = new ColumnElevationRebar();

    obj.CanBeContinued = CanBeContinued;
    obj.CanBeSpliced = CanBeContinued;
    obj.ConnectionType = ConnectionType;
    obj.SpliceLength = SpliceLength;
    obj.Polyline = Polyline;

    return obj;
}

Definition of the class

public class ColumnElevationRebar
    {
        public ColumnElevationRebar Clone()
        {
            var obj = new ColumnElevationRebar();

            obj.CanBeContinued = CanBeContinued;
            obj.CanBeSpliced = CanBeContinued;
            obj.ConnectionType = ConnectionType;
            obj.SpliceLength = SpliceLength;
            obj.Polyline = Polyline;

            return obj;
        }

        private double splicelength;
        public double SpliceLength
        {
            get { return splicelength; }
            set { splicelength = Arithmetics.Ceiling(value, 100); }
        }

        public RebarConnectionType ConnectionType { get; set; }

        public bool CanBeSpliced;
        public bool CanBeContinued;

        public bool IsSelected;

        public Polyline Polyline { get; set; }

        public double Length
        {
            get { return Arithmetics.Ceiling(Polyline.Length, 100); }
        }
    }


public class Polyline
{
    public Polyline Clone()
    {
        var obj = new Polyline {Points = this.Points};
        return obj;
    }

    public Polyline()
    {
        Points = new List<_2DPoint>();
    }

    public List<_2DPoint> Points { get; set; }
}
4
  • can you add the definition of ColumnElevationRebar class? Commented Dec 26, 2014 at 10:45
  • 1
    Seems that Polyline should be cloned, not just assigned. Commented Dec 26, 2014 at 10:47
  • @FelipeOriani I added the definition of the class. Commented Dec 26, 2014 at 10:48
  • You mean I also need to define a Clone method for the polyline class? Commented Dec 26, 2014 at 10:48

2 Answers 2

1

You are making only a shallow copy of the object. You should clone all non-value type objects, if you need a deep copy. Based on the names, I'm assuming only Polyline needs cloning:

public ColumnElevationRebar Clone()
{
    var obj = new ColumnElevationRebar();

    obj.CanBeContinued = CanBeContinued;
    obj.CanBeSpliced = CanBeContinued;
    obj.ConnectionType = ConnectionType;
    obj.SpliceLength = SpliceLength;
    obj.Polyline = Polyline.Clone();

    return obj;
}

Just define the Clone method in the Polyline object (I don't know the class).

Edit: You are making the same mistake in your Polyline class, you also need to clone the list:

public Polyline Clone()
{
    var obj = new Polyline {Points = new List<_2DPoint>(this.Points)};
    return obj;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I did now but it is not working. I added the Polyline class definition to the question.
Thanks. It was really frustrating.
1

It seems Polyline is a class, not primitive type. If so, create Clone method for Polyline too and use it.

6 Comments

Should I use the same Cloning method I have used?
No. You need write new method. As far as I understand, Polyline aggregates an array (or a list) of Points, right? You need create copy of the array in Polyline.Clone method.
One more question Mark, does this apply to enum types as well?
No. Enums like other primitive types (Int32, DateTime) can be just copied, not cloned. String is complex type, but it doesn't require cloning too, because it's read-only. Arrays, lists, stacks and other complex types must be cloned.
var obj = new Polyline {Points = new List<_2DPoint>(this.Points)}; Here will be created new list with same points.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.