Newbie list fun

Hi gents,

I’m having a bit of a time learning about lists. I wonder if you could help. I’m trying to take one object from a list and create an independent copy of it in another list.

//two lists populated with instances of a class
var masterList : List.<EquipmentClass> = new List.<EquipmentClass>();
var secondList : List.<EquipmentClass> = new List.<EquipmentClass>();

function CopyListIndex() {
  //copy index 3 from master list to the end of second list
  secondList.Add(masterList[3]);
}

The result here is that if I edit masterList[3], the new secondList object changes too. Or if I edit the new secondList object, masterList[3] changes.

I would like each copy to be independent.

So, to try and get around this did the following.

//two lists populated with instances of a class
var masterList : List.<EquipmentClass> = new List.<EquipmentClass>();
var secondList : List.<EquipmentClass> = new List.<EquipmentClass>();

function CopyListIndex() {
  //create a temporary list from masterList
  var thirdList : List.<EquipmentClass> = new List.<EquipmentClass>(masterList);
  //copy index 3 from master list to the end of second list
  secondList.Add(thirdList[3]);
  thirdList.Clear();
}

I’m fairly certain that I just did exactly the same thing. At least it does the same as before.

There is another alternative that does work, but it’s very manual. This does what I want, but I have to manually enter all of the object properties.

//two lists populated with instances of a class
var masterList : List.<EquipmentClass> = new List.<EquipmentClass>();
var secondList : List.<EquipmentClass> = new List.<EquipmentClass>();

function ManualEntry() {
  //new equipment class
  var curEquipmentClass = new EquipmentClass();
  //add new equipment class to list
  secondList.Add(curEquipmentClass);
  //fill the whole class manually
  secondList.[secondList.Count - 1].equipName = "stuff"
  secondList.[secondList.Count - 1].equipDescr = "stuff"
  secondList.[secondList.Count - 1].equipId = "stuff"
  //...etc see you in three weeks :)
}

So, how would I go about taking one object from a list and adding it to the end of another list, but breaking that connection between them?

In addition, am I looking for something known as a deep copy? If so, would what I have done above be considered a shallow copy?

Thanks for your time,
Pli

The list items are actually references, and if you shallow copy a reference, the “copy” will refer to the same object as the listitem reference refers to, so it won’t be its own copy at all.

A deep copy would be you creating and calling a function that copies all an objects properties and assigns them to a new objects properties.

Note the Shallow copy and the Deep copy:

    public Person ShallowCopy()
    {
       return (Person)this.MemberwiseClone();
    }

    public Person DeepCopy()
    {
       Person other = (Person) this.MemberwiseClone(); 
       other.IdInfo = new IdInfo(this.IdInfo.IdNumber);
       return other;
    }

JS discussion on various methods:
http://stackoverflow.com/questions/122102/most-efficient-way-to-clone-an-object

Excellent, thanks NA-RA-KU. I’ll see if I can get something similar up and running :slight_smile:

Thanks for the information.

  secondList.Add(new EquipmentClass(thirdList[3]));

usually you create a copy constructor which takes an object of the same class and queries all fields (variables) to initialize the fields of the current instance. so the new keyword is essential here to create a copy (a new instance with its own memory).

Edit: Note that you cannot use constructors on Monobehavior derived Classes (scripts). if your equipment class is a script you must use a method like suggested by NA-RA-KU:

  secondList.Add(new EquipmentClass().Initialize(thirdList[3]));

Thanks you exiguous, very helpful.

Shame there doesn’t seem to be +rep here.

All the best,
Pli