Hi, I'm having a little problem changing members of an object in a list using a found index.
So this is the method I am currently working with:
static void addToInventory(ref List<baseItem> myArray, baseItem item, float maxAmount, ref float currentAmount)
{
if (currentAmount + item.getWeight() <= maxAmount)
{
Console.WriteLine("item.Quantity = {0}", item.Quantity);
if (myArray.Contains(item))
{
Console.WriteLine("Item ({0}) already exists", item.Name);
int id = myArray.IndexOf(item);
myArray[id].Quantity += item.Quantity;//Change occurs in this line, item.Quantity becomes the same as myArray[id].Quantity
}
else
{
Console.WriteLine("Adding new item ({0})", item.Name);
myArray.Add(item);
}
currentAmount += item.getWeight();
}
else
{
Console.WriteLine("Inventory full");
}
myArray.Sort();
}
This method takes several parameters including the inventory/list. I check if the item fits in and if it does, I see if there is another item of the same name in the list, find the index, and add more of the item. However, the quantity of the item added suddenly becomes the same as the quantity of the item in the list. For some reason, this also changes the quantity of the item outside of the list. So therefore, instead of quantities adding up like this: 1, 2, 3, 4, they add up like this: 1, 2, 4, 8. How can I make it so that the quantity of the added item does not change?
I've just started to learn how to use lists so if there is anything I'm missing, don't hesitate to criticize. Thanks in advance.
To Mark: Thanks for the very quick reply! Sorry about the poor naming (myArray); it used to be an ArrayList. The currentAmount and the maxAmount refer to the current weight in the inventory and the max weight the inventory can hold respectively. Also, I don't want to just add 1 to the quantity; I want it to add the quantity of the item I pass in. Thanks for the tips. I'll probably look into using Dictionary instead.