- Home /
check if list contains item with matching string property
im making an inventory of items, i have an items class with each item instance having a name and quantity, i want to add items to the list if the check returns with "nothing found." If i find something and the quantity is 1 or more, i want to increase that items quantity instead of adding a new object.
Im using C#
public void AddItem(string name, int price, string type, int value, string info, int quantity, bool equipped){
for(int I = 0; I < items.Count; I++){
if(items[I].itemName == name){
items[I].quantity += quantity;
}
else{
Item ni = new Item();
ni.itemName = name;
ni.price = price;
ni.type = type;
ni.value = value;
ni.info = info;
ni.quantity = quantity;
ni.equipped = equipped;
items.Add (ni);
}
}
}
You didn't ask 1 question and didn't indicate that something is not working.
Still... If item is already in inventory, it makes more sense to do
items[I].quantity += quantity;
Your current way is only adding 1 to the quantity even if you're trying to add more.
Also, the passed in name parameter is called "iname", but you are assigning "name":
ni.itemName = name;
Either change it to ni.itemName = iname, or change the parameter to "name".
Also, if "items" is an array, you should use items.Length in the for-expression. AFAI$$anonymous$$ .Count is a method, not a member, so it needs the '()'
Sorry hatake3, its morning and i have not slept yet so i were not thinking at full capacity. What you say is all true, and i wanted to do this in the way you described. The problem is, i dont know how to write my code so that it adds a new item if there is no current item with a property of the same 'name' as the one im trying to add. Hope that cleared things up, sorry again, and thank you for your reply.
Oh and im using Generic List. Count is the size of the List. http://msdn.microsoft.com/en-us/library/27b47ht3(v=vs.110).aspx
Oh, that was crafty. It worked without needing any editing. Its such a simple thing too. I cannot believe i didn't think about using a bool. Thank you for you help! Convert it to and answer and i shall accept it.
Answer by hatake3 · Sep 21, 2014 at 11:54 AM
Ah, I actually didn't notice that. What you're doing now is going through the items in "items", and for every item, if it is not the same as the item you're trying to add, it will add it. So if you have a list of 10 items and try to add an item that is not in the list, you will add the item 10 times.
You could do something like this:
public void AddItem(string name, int price, string type, int value, string info, int quantity, bool equipped){
bool isInList = false;
for(int I = 0; I < items.Count; I++){
if(items[I].itemName == name){
items[I].quantity += quantity;
isInList = true;
}
}
// Outside for loop
if (isInList == false){
Item ni = new Item();
ni.itemName = name;
ni.price = price;
ni.type = type;
ni.value = value;
ni.info = info;
ni.quantity = quantity;
ni.equipped = equipped;
items.Add (ni);
}
}
Noticed you're using a list, and ofc items.Count is fine
Answer by dmg0600 · Sep 21, 2014 at 11:59 AM
Using Linq this is pretty easy. You would have to add using System.Linq;
public void AddItem(string iname, int price, string type, int value, string info, int quantity, bool equipped){
Item foundItem = items.FirstOrDefault(i => i.itemName == iname);
if (foundItem != null)
{
Item ni = new Item();
ni.itemName = name;
ni.price = price;
ni.type = type;
ni.value = value;
ni.info = info;
ni.quantity = quantity;
ni.equipped = equipped;
items.Add(ni);
}
else
foundItem.quantity++;
}
This way it will look for the first item in the list with that name. If there is no item with that name, it will create a new one and add it to the list, if the item is found it will increase its quantity.
Oh a second method, i did not see this one. I will look at this method also.
ok, i read up on FirstOrDefault (here: http://msdn.microsoft.com/en-us/library/vstudio/bb340482(v=vs.100).aspx) and found that it worked too. I can't accept two answers as correct so i voted up this reply and accepted the first answer. Ive learned from both of your answers. Thank you for taking the time to $$anonymous$$ch me.
Hi. I used this code and it worked, thanks! However it fails if there is ever no such item in my inventory (object reference error). Is there a way around this? Also is there a way of doing this multiple times without defining 3 separate 'foundItems'?
I started learning to code this year in 2017 so forgive my ignorance.
Orders.currentOrder = orders.FirstOrDefault (i => i.orderName == orderDrpdown.captionText.text);
Your answer
Follow this Question
Related Questions
Using enums to design an inventory system? 1 Answer
How can I remove an item ffrom a list. 1 Answer
Count object list duplicates 1 Answer
Decorator pattern for inventory item class 0 Answers
Multiple Cars not working 1 Answer