C# Error Not All Code Path Returns Value
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow not all code paths return a value try catch Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is not all code paths return a value unity a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up c# returning error “not all not all code paths return a value ienumerator code paths return a value” up vote 17 down vote favorite 6 I'm trying to write code that returns whether or not a given integer is or is not divisible evenly by 1 to 20, but i keep receiving "error CS0161: 'ProblemFive.isTwenty(int)': not not all code paths return a value in mvc all code paths return a value" Please help. Here is my code: public static bool isTwenty(int num) { for(int j = 1; j <= 20; j++) { if(num % j != 0) { return false; } else if(num % j == 0 && num == 20) { return true; } } } c# share|improve this question asked Jan 17 '14 at 20:47 user115185 89114 migrated from programmers.stackexchange.com Jan 17 '14 at 22:48 This question came from our site for professional programmers interested in conceptual questions about software development.
Not All Code Paths Return A Value Typescript
5 Your code doesn't make sense but the error message is quite clear. You need to return a value even if both your if conditions are false for 20 iterations. –ChaosPandion Jan 17 '14 at 20:52 4 You're thinking like a human, not a compiler. The compiler doesn't know what you are thinking, or how the logic is supposed to flow (past optimization). What happens if a value doesnt match either 'if'? –GrandmasterB Jan 17 '14 at 21:08 add a comment| 6 Answers 6 active oldest votes up vote 34 down vote You're missing a return statement. When the compiler looks at your code, it's sees a third path (the else you didn't code for) that could occur but doesn't return a value. Hence not all code paths return a value. For my suggested fix, I put a return after your loop ends. The other obvious spot - adding an else that had a return value to the if-else-if - would break the for loop. public static bool isTwenty(int num) { for(int j = 1; j <= 20; j++) { if(num % j != 0) { return false; } else if(num % j == 0 && num == 20) { return true; } } return false; //This is your missing statement } share|improve this answer edited Jan 18 '14 at 14:01 answered Jan 17 '14 at 20:53 GlenH7 1,6011531 1 This returns the wrong result for isTwenty(44696171520). It should return true becase it can be evenly divided by all numbers 1 to 20, but it returns fa
here for a quick overview of the site Help Center Detailed answers to any questions you might
How To Fix Not All Code Paths Return A Value
have Meta Discuss the workings and policies of this site About not all code paths return a value javascript Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting not all code paths return a value in wcf service ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 http://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error: Not all code paths return a value up vote 2 down vote favorite Okay I wrote the following code to count the node of an XML file: private Dictionary
Answers Feedback Issue Tracker Blog Evangelists User Groups Navigation Home Unity Industries Showcase Learn Community Forums Answers Feedback Issue Tracker Blog Evangelists User Groups Get Unity Asset Store Unity account You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity http://answers.unity3d.com/questions/1034385/c-not-all-code-paths-return-a-value.html 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 / 0 Question by TheBlackBox · Aug 23, 2015 at 10:07 PM · c#errorcontrollerpathsreturn value [C#] Not all code paths return a value Hey there, So I'm working on some random dungeon-gen, and up until now it's being going great. I am getting the error Not all code paths return a not all value I looked into the error, tweaked my code a little and I'm still not making any progress. Let me post my code. using UnityEngine; using System.Collections.Generic; public class DTileMap { protected class DRoom { public int left; public int top; public int width; public int height; public int right { get {return left + width - 1;} } public int bottom { get { return top + height - 1; } } public bool CollidesWith(DRoom other) { if( left > other.right-1 ) return not all code false; if( top > other.bottom-1 ) return false; if( right < other.left+1 ) return false; if( bottom < other.top+1 ) return false; return true; } } int size_x; int size_y; int[,] map_data; List