Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
Add comment · 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 $$anonymous$$onoDevelop... don't have VS2015 to hand.

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

$$anonymous$$y VS2015 doesn't give any error on that code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 which object (this or the other) is greater. positive number mean the "other" is greater, while negatives means its lesser than this instance. zero means "this" and "other" are equal. anything 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 this 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 things 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
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

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

FPS TUTORIAL: AI robot not taking damage. 0 Answers

2d tutorial error 2 Answers

learn c#? please help. 1 Answer

Questions about Lightning, Navigation , Tutorialsource 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