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 /
  • Help Room /
avatar image
0
Question by mightybob · Nov 08, 2016 at 10:14 PM · functionbooleanfor-loopreturnreturn value

Not all code paths return a value error. Fixing the error breaks my function. Ideas?

Not sure how to solve this issue. If I use this code that I made:

 public bool ContainsCursing(string name) {
 
         for (int i = 0; i < ScreenedWords.Length; i++) {
             if (name.ToLower ().Contains (ScreenedWords [i].ToLower ())) {
                 return true;
             } else if (i >= ScreenedWords.Length) {
                 return false;
             } 
         }
     }

I get the error: "Not all code paths return a value". I can fix the error by adding "return true" outside of the for loop. But then, instead of being a syntax error, it's a logic error (if you get what I mean). Now my function will always return true no matter what. So how can I fix this? I tried "return null", that gives me the error "Cannot convert null to `bool' because it is a value type". The function only has 2 possible code paths, and both of them return a value so I'm not sure why Unity can't recognize that. Any ideas, people? Thanks.

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 Filhanteraren · Nov 08, 2016 at 10:30 PM 0
Share

If you remove the else if statement and add return false outside the for loop it should function correct.

avatar image Owen-Reynolds · Nov 08, 2016 at 10:34 PM 0
Share

C# is frequently wrong about this (well, really the compiler.) You can have [if(x==1) return true; if(x!=1) return false] and it isn't smart enough to know you've covered every case.

(But in this case, Suddoha is correct - it really is a problem with your code.)

1 Reply

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

Answer by Suddoha · Nov 08, 2016 at 10:29 PM

The function does not only have 2 possible paths. The for loop might not even run once if the ScreenedWords.Length is 0. So that's a case which is not being covered here.

Even if it executes, the 'else if' kills it all, because 'else if' is not 'else'. There's still a condition to be met (even if yours will never be met in a single-threaded (or correctly multi-threaded, i.e. thread-safe) environment).

And that's because you've got a logical error in your existing code.

Assuming that there's no other thread which could (theoretically) remove items during the iteration, the 'else if' part will never be reached as you only ever iterate as long as ' i < ScreenedWords.Length ' which excludes ' i >= ScreenedWords.Length '.

This being said, just let that case alone, it's covered by the case that no match has been found (which also includes that the for loop doesn't even run once) and that can be written as:

 public bool ContainsCursing(string name) 
 {
     for (int i = 0; i < ScreenedWords.Length; i++) 
     {
         if (name.ToLower ().Contains (ScreenedWords [i].ToLower ())) 
         {
             return true;
         } 
     }
     return false;
 }
Comment
Add comment · Show 1 · 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 mightybob · Nov 08, 2016 at 10:42 PM 0
Share

Thanks for the explanation. I haven't needed to write a custom function with a returning value in a long time.

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

60 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 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 avatar image avatar image avatar image avatar image

Related Questions

Using function return in if statement 2 Answers

Script not returning value to other script 1 Answer

method return type is incompatible 0 Answers

My Bool function is returning False but it should be true in my opinion. Csharp C# 0 Answers

PROBLEM WITH ANIM.SETBOOL NOT WORKING 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