• 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 davidsabens · Jan 31, 2016 at 02:41 PM · tutoriallists

Re: Unity tutorial on Lists, not all code paths return a value

I followed the unity tutorials on lists. Aside from some variable name differences, I have copied the code exactly into VS2015 and cannot seen to get it to work. The code is simple: check to see if there is a value present in the other list, if not set to "-1" and for all other circumstances set the value to match the second list. VS pops an error " 'DefineDropList.CompareTo(DefineDropList)' : not all code paths return a value. Any insight would be greatly appreciated.

 public class DefineDropList : IComparable<DefineDropList>
 {
     public string name;
     public int trophyenabled;
 
     public DefineDropList(string newName, int newTrophyEnabled)
     {
         name = newName;
         trophyenabled = newTrophyEnabled;
     }
 
     public int CompareTo(DefineDropList other)
     {
         if (other == null)
         {
             trophyenabled = -1;
             return trophyenabled;
         }
         trophyenabled = other.trophyenabled;
         return trophyenabled;
     }
 }

Comment

People who like this

0 Show 2
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 gorsefan · Jan 31, 2016 at 06:20 PM 0
Share

Not sure if this helps but, after resolving IComparable to System.IComparable, that works fine for me in MonoDevelop... don't have VS2015 to hand.

avatar image Teravisor · Jan 31, 2016 at 09:51 PM 0
Share

My VS2015 doesn't give any error on that code.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by JoshuaMcKenzie · Jan 31, 2016 at 11:22 PM

Thats not how IComparable is meant to be used, its meant to help a classes know w$$anonymous$$ch object (t$$anonymous$$s or the other) is greater. positive number mean the "other" is greater, w$$anonymous$$le negatives means its lesser than t$$anonymous$$s instance. zero means "t$$anonymous$$s" and "other" are equal. anyt$$anonymous$$ng can use the IComparable to make comparisons between two objects, but typically built-in Sorting methods use it the most

You shouldn't have the CompareTo directly modifying any class variables since you don't know exactly how a sort method will iterate through each instance of your DefineDropList (unless you REALLY know what your doing, even then there is usually better ways than IComparable). There are a myriad of different types of sorts out there and many will often access the same instance's CompareTo multiple times. with code like that you could have some sorts get caught in an infinite loop since the CompareTo wouldn't return consistent values between the two same instances.

try a different function (or remove the IComparable interface if you're not sorting t$$anonymous$$s class)

     public static int GetOtherTrophy(DefineDropList other)
     {
         if(other == null)
             return -1;
     
         return other.trophyEnabled;
     }

Edit: Forgot that I didn't quite answer your question. The code you shown us compiles properly. your specific issue can stem from other t$$anonymous$$ngs you haven't shown us. like what namespaces you are using, if the filename matches the classname, and maybe even re-syncing the solution to your project.

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

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

6 People are following this question.

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

Related Questions

I'm trying to make a list driven menu for items, skills that meet a interactable interface like the one in Persona 3 Portable, can someone help? 0 Answers

Side battle tutorial using 3d objects 0 Answers

Enemy AI script trouble(Js to c#) 3 Answers

Stealth project: lightmapping artifacts 1 Answer

Why is the boundary not working? 1 Answer


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