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