Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy an instance of an object?

Tags:

c#

.net

oop

I'm trying to write some code that populates a List (actually, it's a series of Lists, but we can pretend it's just one List). The idea is to add an IPackage to the List for the total quantity of IPackage on order. See the following code:

        ParseExcel pe = new ParseExcel();
        Pinnacle p = pe.ParsePinnacleExcel();
        Rack r = new Rack(20,2,4.5,96,42,6,25*12);
        foreach (PinnacleStock ps in p.StockList.Where(x => 
                 x.ColorCode == "10" && 
                 x.PackageLength == 30.64))
        {
            for (int i = 1; i <= ps.OnOrder; i++)
            {
                r.TryAddPackage((IPackage)ps);
            }
        }

Everything seems to be working well, insofar as the IPackage is repeatedly added to the list. However, it seems that the same instance of the object is being added, i.e. the object is not being copied each time it's added to the list.

What do I need to do to ensure that a copy of the object is inserted into the list, and not just an additional reference?

like image 904
Ben McCormack Avatar asked Jan 21 '10 03:01

Ben McCormack


People also ask

How do you copy an instance of an object in Python?

Copy an Object in Python In Python, we use = operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object.

How do I clone an instance in Java?

Creating a copy using the clone() methodclone() to obtain the cloned object reference. The class must also implement java. lang. Cloneable interface whose object clone we want to create otherwise it will throw CloneNotSupportedException when clone method is called on that class's object.

How do you copy an existing object?

To create a copy of an existing object in the vault, right-click the object of your choice and select Make Copy from the context menu. This command creates an entirely new object using the metadata and contents of the source object.


2 Answers

Then you need to implement ICloneable and replace

r.TryAddPackage((IPackage)ps);

with

r.TryAddPackage((IPackage)ps.Clone());

It's up to you to decide how Clone should populate the new instance of PinnacleStock that it returns.

At the most basic level, you could say

public PinnacleStock : ICloneable {
    public PinnacleStock Clone() {
        return (PinnacleStock)this.MemberwiseClone();
    }
    object ICloneable.Clone() {
        return Clone();
    }
    // details
}

This will just do a shallow copy of PinnacleStock. Only you know if this is the correct semantics for your domain.

like image 131
jason Avatar answered Oct 05 '22 23:10

jason


If you only need a shallow copy, then you can write a quick-fix clone method:

public class PinnacleStock : ICloneable
{
    public PinnacleStock Clone()
    {
        return (PinnacleStock)this.MemberwiseClone();
    }

    object ICloneable.Clone()
    {
        return Clone();
    }

    // Other methods
}

If you need a deep copy (i.e. if PinnacleStock has sub-objects that you want to be copied as well), then you will need to write one yourself.

like image 37
Aaronaught Avatar answered Oct 05 '22 22:10

Aaronaught