• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Punkjim420 · Sep 21, 2014 at 11:13 AM · c#listinventorypropertiesitems

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 "not$$anonymous$$ng found." If i find somet$$anonymous$$ng 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);
                 
             }
         }
     }

 
Comment

People who like this

0 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hatake3 · Sep 21, 2014 at 11:30 AM 0
Share

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. AFAIK .Count is a method, not a member, so it needs the '()'

avatar image Punkjim420 · Sep 21, 2014 at 11:40 AM 0
Share

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.

avatar image Punkjim420 · Sep 21, 2014 at 11:46 AM 1
Share

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

avatar image Punkjim420 · Sep 21, 2014 at 12:09 PM 0
Share

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.

avatar image hatake3 · Sep 21, 2014 at 12:16 PM 0
Share

Thanks :-)

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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 somet$$anonymous$$ng like t$$anonymous$$s:

 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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by dmg0600 · Sep 21, 2014 at 11:59 AM

Using Linq t$$anonymous$$s 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++;
 }

T$$anonymous$$s 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.

Comment
Punkjim420

People who like this

1 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Punkjim420 · Sep 21, 2014 at 12:10 PM 0
Share

Oh a second method, i did not see this one. I will look at this method also.

avatar image Punkjim420 · Sep 21, 2014 at 12:20 PM 0
Share

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 teach me.

avatar image OtisMiller · Apr 22, 2017 at 03:49 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

stack by quanity...? C# 2 Answers

Method is called, but GUI doesn't show up 1 Answer

How to make an inventory for my text advenutre? 0 Answers

Inventory Script. List Contains. I dont know the need bit of code.[UNSOLVED] 2 Answers

Database error updating inventory 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges