Not All Code Path Return Value Error
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 Error In C#
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 try catch a community of 6.2 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 in mvc 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 Asp.net C#
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 professionals, academics, and students working within the systems development
Not All Code Paths Return A Value In Asp.net Mvc
life cycle who care about creating, delivering, and maintaining software responsibly. 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,6071531 1 This returns the wrong result for isTwenty(44696171520). It should return true becase it can be ev
here for a quick overview of the site Help Center Detailed answers to not all code paths return a value unity any questions you might have Meta Discuss the workings and
Not All Code Paths Return A Value Unity C#
policies of this site About Us Learn more about Stack Overflow the company Business Learn more how to fix not all code paths return a value about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community http://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Getting Error: Not all code paths return a value up vote 3 down vote favorite I am new to mvc C# and am stuck. Please advise on http://stackoverflow.com/questions/10731097/getting-error-not-all-code-paths-return-a-value how to fix this. I'm getting the error on Add. When I hover over the red squiggly line it says "Not all code paths return a value" public ActionResult Add(ShapeInputModel dto, FormCollection collection) { var model = new GeoRegions(); if (TryUpdateModel(model)) { var destinationFolder = Server.MapPath("/App_Data/KML"); var postedFile = dto.Shape; if (postedFile != null) { var fileName = Path.GetFileName(postedFile.FileName); var path = Path.Combine(destinationFolder, fileName); postedFile.SaveAs(path); //Save to Database Db.AddGeoRegions(model); return RedirectToAction("Index"); } return View(); } } c# share|improve this question edited May 24 '12 at 4:21 Icarus 45.7k85486 asked May 24 '12 at 4:20 user1382770 97129 add a comment| 10 Answers 10 active oldest votes up vote 3 down vote accepted Use This : public ActionResult Add(ShapeInputModel dto, FormCollection collection) { var model = new GeoRegions(); if (TryUpdateModel(model)) { var destinationFolder = Server.MapPath("/App_Data/KML"); var postedFile = dto.Shape; if (postedFile != null) { var fileName = Path.GetFileName(postedFile.FileName); var path = Path.Combine(destinationFolder, fileName); postedFile.Sav
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 All Questions... Linux questions C# questions ASP.NET http://www.codeproject.com/Questions/443341/Not-All-Code-Path-Return-A-Value questions SQL questions fabric 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 https://www.daniweb.com/programming/software-development/threads/365710/c-error-not-all-code-paths-return-a-value / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System Admin Hosting and Servers Java .NET Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / not all 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 Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise not all code with us About our Advertising Employment Opportunities About Us Ask a Question All Questions All Unanswered FAQ Not All Code Path Return A Value Rate this: Please Sign up or sign in to vote. See more: C# ASP.NET .NET Dear Friends, I have written a code like this to check the access level. But I am getting the error as "Not All Code Path Return A Value" Kindly suggest the reason and solution for the same protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (isAccessLevel() == "YES") { Show_My_Data() } else { Hide_My_Data() } } } public string isAccessLevel() { if (ACC_YN == "YES") { if (STA_YN == "RELEASE") { return "YES"; } else { return "NO"; } } } Posted 20-Aug-12 0:44am Arunprasath Natarajan3.4K Add a Solution 7 solutions Top Rated Most Recent Rate this: Please Sign up or sign in to vote. Solution 1 Accept Solution Reject Solution isAccessLevel is not returning the value in from all branches. Change it to like this, public string isAccessLevel() { if (ACC_YN == "YES") { if (STA_YN == "RELEASE") { return "YES"; } else { return "NO"; } } else { return "NO"; } } Permalink Posted 20-Aug-12 0:47am pramodhegde8814.9K Rate this: Please Sign up or sign in to
help me. I'm trying to pass the name of the user who logs in from a login form and display his/her name on a Main form. I'm using get{} and set{}. My problem is with the function I'm trying to create to return the name of the user which I'm going to place inside my get{}. Here's my function (I've placed this within a Class): public static string loggedinuser(TextBox uname,TextBox password) { readrecord("Select dbo.loggeduser('" + uname.Text + "','" + password.Text + "')"); if (dr.Read()) { while (dr.Read()) { string name =(dr[0].ToString()); return name; } } con.Close(); } It's showing this error "Not all code paths return a value." I don't understand. I already have the return statement. If somebody can help me, I'm most grateful. Thanks so much in advance! c# Chair 17 34 posts since Jan 2011 Community Member 9Contributors 11Replies 89Views 5 YearsDiscussion Span 2 Years Ago Last Post by pritaeas 0 Mitja Bonca 557 5 Years Ago Your code does not return the name string (value) from all over the method. It reutrns only from the while loop. What in case if the code does not go to while loop? It will return what? notihng. But your method return type is a string. That means you have to return string even if the code does not go to while loop. Do it like this: public static string loggedinuser(TextBox uname,TextBox password) { string name = null; readrecord("Select dbo.loggeduser('" + uname.Text + "','" + password.Text + "')"); if (dr.Read()) { while (dr.Read()) { name =(dr[0].ToString()); } } con.Close(); return name; } In case if the code will no go into while loop, it will return a string with null value. So on the other side you can check it: string _name = loggeddinuser(uname, password); if(_uname != null) { //your code if the string returns a not null string! } else MessageBox.Show("Value is null."); //or do some other user notification. 0 skatamatic 371 5 Years Ago The return statement will not be called if the first if() statement fails (ie the Read() returns false) One way to solve this is to put return ""; at the end (after con.close()) Note that con.close() is not being called if the name is read, since return is called which jumps out of the function. Here's what I would do: public static string loggedinuser(TextBox uname,TextBox password) { readrec