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 error not all code paths return a value in c# net the company Business Learn more about hiring developers or posting ads with us Stack error 1 not all code paths return a value Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community not all code paths return a value javascript of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Cause of Error CS0161: not all code paths return a value up vote 21 down vote favorite not all code paths return a value c# mvc 1 I've made a basic extension method to add retry functionality to my HttpClient.PostAsync: public static async Task
Not All Code Paths Return A Value In Lambda Expression
catch (HttpRequestException) { ++attempt; if (attempt > maxAttempts) throw; } } } The above code gives me the following error: Error CS0161 'HttpClientExtensions.PostWithRetryAsync(HttpClient, Uri, HttpContent, int, Action)': not all code paths return a value. If I add throw new InvalidOperationException() at the end (or return null for that matter), the error goes away as expected. What I'd really like to know is: is there any code path that actually exits this method without either a value being returned or an exception being thrown? I can't see it. Do I know more than the compiler in this case, or is it the other way around? c# .net dotnet-httpclient share|improve this question edited Nov 9 '15 at 10:18 Thomas Ayoub 16.9k103884 asked Nov 9 '15 at 10:15 Martin Wedvich 960619 Funny question :) –Thomas Ayoub Nov 9 '15 at 10:20 Check stackoverflow.com/questions/22993131/… –MusicLovingIndianGirl Nov 9 '15 at 10:21 3 I would suspect that it's unable to analyse/prove the fact that either the catch block throws or the while block will not terminate. –Damien_The_Unbeliever Nov 9 '15 at 10:25 7 Simply change while (attempt <= maxAttempts) to while(true). –Henrik Nov 9 '15 at 10:34 add a comment| 4 Answers 4 active oldest votes up vote 39 down vote accepted T
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
Not All Code Paths Return A Value Unity
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question not all code paths return a value task x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join not all code paths return a value switch them; it only takes a minute: Sign up C#: Error. Not all code paths return a value up vote 0 down vote favorite I'm working on a project in C# and I have created the following function: static bool detectIfRepeated(OleDbConnection http://stackoverflow.com/questions/33606833/cause-of-errorcs0161-not-all-code-paths-return-a-value cnx, String username) { DataTable res = new DataTable(); OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM UserPassword", cnx); adp.Fill(res); int i = 0; bool found = false; String user = Convert.ToString(res.Rows[i]["User"]); while (i < res.Rows.Count && !found) { if (user == username) { found = true; MessageBox.Show("The username is already taken. Choose a different one."); return true; } else i++; } if (!found) return false; } However, I don't know why (I've been trying to look for the mistake http://stackoverflow.com/questions/27079242/c-error-not-all-code-paths-return-a-value for one hour) I'm getting the error: Not all code paths return a value. I can't see why not, the program navigates on the list if it finds that usernames are the same it returns true; if it has not find any coincidence after the whole loop it returns false. Please can anyone help me find it? I'm sure it has to be a silly mistake but I am getting crazy... Thank you so much!! c# loops search share|improve this question edited Nov 22 '14 at 15:53 Sam Greenhalgh 3,956829 asked Nov 22 '14 at 15:46 user3780731 456 1 I'm not able to see the reason for the "found" variable in this code. When you initiate it, you initiate it to false, the only time you change it is right before you return true, and then you're not checking against it. Also, the only time you're checking the value of found is when you're outside the loop, but then it's guaranteed to be false (since the only place you change it on is right before a return) So, what I'm trying to say is that you could remove the found variable and the lines that change or check it and your code would do the exact same thing (without the compiler complaining) –Fabian Miiro Nov 22 '14 at 16:01 add a comment| 3 Answers 3 active oldest votes up vote 0 down vote
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... C# questions Linux questions ASP.NET questions SQL questions http://www.codeproject.com/Questions/443341/Not-All-Code-Path-Return-A-Value 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 http://forums.asp.net/t/1002308.aspx?+C+not+all+code+paths+return+a+value 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 / Suggestions Spam and Abuse Watch features not all 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 with us About our Advertising Employment Opportunities About Us not all code 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.8K Rate this: Please Sign up or sign in to vote. Solution 2 Accept Solution Reject Solution You should have return variable outside of if else loop. think w
ASP.NET Community Standup Forums Help Home/ASP.NET Forums/.NET Languages/C#/C# :not all code paths return a value C# :not all code paths return a value RSS 4 replies Last post Jan 07, 2008 05:20 AM by vishalsharma.net ‹ Previous Thread|Next Thread › Print Share Twitter Facebook Email Shortcuts Active Threads Unanswered Threads Unresolved Threads Support Options Advanced Search Reply msraja Member 1 Points 56 Posts C# :not all code paths return a value Jun 23, 2006 09:39 AM|msraja|LINK I got the error " not all code paths return a value ". While i execute the below the functions. Plz give the solution for that.... Thanks iin advance Raja private static int GetPBAID(string campaignIDList) { int intPBACount; using (SqlConnection conn = new SqlConnection("Context Connection=true")) { try { SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = conn; sqlCmd.CommandText = " SELECT Count(Distinct(PBA_ID))as PBACount from RD_Campaign" + " WHERE CampaignID IN (" + campaignIDList + ") AND PBA_ID is not null"; sqlCmd.Connection = conn; conn.Open(); intPBACount = (int)sqlCmd.ExecuteScalar(); conn.Close(); return intPBACount; } catch (Exception ex) { SqlContext.Pipe.Send(ex.Message); } } } Thanks, Regards, Raja msrasa@yahoo.com Reply DWesthead Member 362 Points 231 Posts Re: C# :not all code paths return a value Jun 23, 2006 10:25 AM|DWesthead|LINK If an exception is caught then your method isnt returning anything - try adding return -1 to the catch block Please remember to click “Mark as Answer” on the post, if it helps you. Reply e_screw Star 9619 Points 3854 Posts Re: C# :not all code paths return a value Jun 24, 2006 04:16 AM|e_screw|LINK Change your code this way, so it will return