Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Raux · Jul 15, 2014 at 12:23 PM · listmethodcontains

List.Contains Method wont work.

I have a list called selMods. It's a list of type Option which is a class I made that just contains some information. Here is the class in it's entirety.

 public class Option {
     
     public string Name;
     public bool IsActive = true;
     public int ID;
 
     private Option(){}
 
     public Option(string name){
         Name = name;
     }
 
     public Option(string name, int id){
         Name = name;
         ID = id;
     }
 }

The selMods list is filled with Options that only have a string name specified. The ID for these options is always null. (There are other options that do require an ID.)

The list is populated depending on what the user selected. In some cases I need to know if a certain option was selected. For example an option with the string name "Disable Research". Now I know for a fact that selMods contains an Option with the name "Disable Research", I selected that option myself. But when I want to check it in the code I can't manage to get it work.

I tried 2 things, based on the documentation for List.Contains List.Contains .

     if(selMods.Contains(new Option { Name = "Disable Research"})){
         Debug.Log("Found Disable Research");
     }

If I try it this way, I simply get the following error: Assets/Scripts/Generator.cs(285,77): error CS1729: The type Option' does not contain a constructor that takes 0' arguments

It has a private constructor that takes 0 arguments, which is purely for XMLSerialization stuff, but I don't understand why it gives me this error. I'm not looking for an Option with 0 arguments, I'm looking for an Option with 1 argument, right?

The second thing I tried is:

 if(selMods.Contains(new Option ("Disable Research"))){
     Debug.Log("Found Disable Research");
 }

This doesn't give me any errors but the debug.log is never run, even though I know for a fact that the option is inside selMods.

Could somebody be so kind to tell me what I did wrong? I just need to check if the list contains an option with the string name "Disable Research".

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Landern · Jul 15, 2014 at 12:37 PM

So if you look at the documentation from MSDN, you will notice that they Implemented IEquatable on the Part class. This is VERY VERY important as they override the general ability of List.Contains method that would compare references(if of reference type) and had it compare PartId's to check to see if the list contains a Part object with that part type. Again without a comparer implemented it will compare the internal references(does it point to the same object).

Now you could use LINQ to get the job done.

 using System.Linq; // <-- you need that name space
 
 ...
 
 if (selMods.Any(opt => opt.Name.Equals("Disable Research", StringComparison.InvariantCultureIgnoreCase))) {
   Debug.Log("Found Disabled Research");
 }
 
 ...


In the case above, we iterated over the collection(read List) and checked each item for an Option object with a name of "Disable Research", case-insensitive.

List.Contains is not the same as a string.Contains.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

operator == is not working every time to find if a list contains item in another list 1 Answer

How to search a certain variable in a list of a created class (C#) 1 Answer

How to determine if your List has more than 1 of the same object listed? 1 Answer

Inventory Script. List Contains. I dont know the need bit of code.[UNSOLVED] 2 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