C# Error Not All Code Paths Return A 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 error 1 not all code paths return a value c# Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community
C# Not All Code Paths Return A Value Try Catch
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up c# not all code paths return a value c# mvc returning error “not all 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 not all code paths return a value in asp.net c# receiving "error CS0161: 'ProblemFive.isTwenty(int)': not 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
Not All Code Paths Return A Value Try Catch
programmers interested in conceptual questions about software development. 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 beca
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 not all code paths return a value unity Stack Overflow the company Business Learn more about hiring developers or posting ads not all code paths return a value in mvc with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
Not All Code Paths Return A Value Ienumerator
is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why am I getting this error: not all code paths return a value? http://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value up vote 0 down vote favorite 1 hi im new to c# and was trying to code but getting error can anybody help me with this what am i doing wrong? using System; using System.Collections.Generic; using System.Text; namespace hodder { class Program { public static void Main() { isHodder(3); } static int isHodder(int n) { int k = n; for (int i = 2; i <= n / 2;i++ ) http://stackoverflow.com/questions/929608/why-am-i-getting-this-error-not-all-code-paths-return-a-value { if ((n % 1) == 0) { return 0; } else { for (int j = 2; j <= k;j++ ) { if (n == (2^ j) - 1) { return 1; } else { return 0; } k=(2^j)-1; } } } } } } im getting error on " static int isHodder(int n) " 'hodder.Program.isHodder(int)': not all code paths return a value and "Unreachable code detected " on "k=(2^j)-1" c# .net visual-studio-2005 console-application share|improve this question edited May 30 '09 at 13:16 Jon B 33.7k2499142 asked May 30 '09 at 11:52 jarus 95462964 1 the error would be helpful. –kenny May 30 '09 at 11:55 i think its good practice to only have one return statement at the end of functions. easier to read and to debug. –UrbanEsc Jul 19 '11 at 6:45 add a comment| 5 Answers 5 active oldest votes up vote 7 down vote accepted The first error, "not all code paths return a value" means there is a path that the code could follow where no value would be returned (ie: calling isHodder(1)). You need to return some value outside of the for loop. Additionally, since you have an if/else block inside the second for loop the lin
Tips/Tricks Top Articles Beginner Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your Blog quick answersQ&A Ask a Question View Unanswered Questions View http://www.codeproject.com/Questions/303345/Not-all-code-paths-return-a-value-Please-explain-a All Questions... C# questions Linux questions ASP.NET questions SQL questions VB.NET questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System Admin Hosting and Servers Java .NET not all Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News The Weird & The Wonderful The Soapbox Press Releases Non-English Language > General Indian not all code Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise with us About our Advertising Employment Opportunities About Us Ask a Question All Questions All Unanswered FAQ "Not all code paths return a value". Please explain about this error. Rate this: Please Sign up or sign in to vote. See more: C# "Not all code paths return a value". Please explain about this error. Posted 20-Dec-11 19:47pm maajanes698 Add a Solution Comments RaisKazi 21-Dec-11 1:48am Post your code. Al Moje 21-Dec-11 1:54am You made a function or method that expects to return an explicit value but you fail to do... 4 solutions Top Rated Most Recent Rate this: Please Sign up or sign in to vote. Solution 1 Accept Solution Reject Solution This kind of error generally occurs when you are branching out your code (using if-else or switch case) and then returning values from some of the conditions but not all. This is the compiler error CS0101 and you can read more about it here[^]. They have a sample that should explain the error to y