473,394 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Not all code paths return a value [c#]

Markus
6,050 Expert 4TB
I have a method that checks whether the passed argument is present in an array. The method needs to return a bool value. Take a look at said method:

Expand|Select|Wrap|Line Numbers
  1. private bool IPExist(string IP)
  2.         {
  3.             // We need to check if the IP exists in the
  4.             // IP array
  5.             foreach (string x in IPs)
  6.             {
  7.                 if (x == IP)
  8.                 {
  9.                     // IP exists
  10.                     return false;
  11.                 }
  12.                 else return true;
  13.             }
  14.         }
  15.  
This gives me the error of 'not all code paths return value'. If I insert a 'return true' after the foreach loop, the function always returns true. Likewise if I insert 'return false' the function always returns false.

How can I write this function to give the desired effect?
Nov 3 '08 #1
4 10251
nateraaaa
663 Expert 512MB
You also need to return true or false outside of your foreach loop.

Nathan
Nov 3 '08 #2
Curtis Rutland
3,256 Expert 2GB
The way I usually handle these situations, is I make a bool called flag or something, and set it to false at the beginning of the loop. Then, if my condition becomes true in the loop, I set it to true. Then I return the value. If you want, you can use the "break" command to exit the loop.
Nov 3 '08 #3
Markus
6,050 Expert 4TB
You also need to return true or false outside of your foreach loop.

Nathan
As I said in my post above: If I do this the result is always that of the last return value.
Nov 3 '08 #4
Markus
6,050 Expert 4TB
The way I usually handle these situations, is I make a bool called flag or something, and set it to false at the beginning of the loop. Then, if my condition becomes true in the loop, I set it to true. Then I return the value. If you want, you can use the "break" command to exit the loop.
Disregard my post. *embarrased*

I changed it to this (took away the else)
Expand|Select|Wrap|Line Numbers
  1. private bool IPExist(string IP)
  2.         {
  3.             // We need to check if the IP exists in the
  4.             // IP array
  5.             foreach (string x in IPs)
  6.             {
  7.                 if (x == IP)
  8.                 {
  9.                     // IP exists
  10.                     return true;
  11.                 }
  12.             }
  13.             return false;
  14.         }
and it works.
Nov 3 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
6
by: Bruce W.1 | last post by:
The intent of my web service is an RSS feed from a blog. Originally I used a StringBuilder to make the XML and returned a string from the webmethod. But this doesn't display properly in IE. So...
5
by: n_o_s_p_a__m | last post by:
Can't compile. Does this mean that all functions that throw exceptions must be of return type void? examples: // won't compile: "not all code paths return a value" public override int Run() {...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
4
by: OutdoorGuy | last post by:
Greetings, I am attempting to compile the code below, but I am receiving an error message when I do so. The error message is: "CSO161: 'Forloop.CalcAvg(int)': Not all code paths return a...
3
by: Oberon | last post by:
How do I deal with this? I am getting an error for each get in the Game class (see code below). In the simplified example below I have reduced this to just 3 fields, one which can be NULL. I...
7
by: Robert | last post by:
I have the function below. it returns a "simpleresult" which I've also included the definition of below. In VS2005 (after upgrading the project), I get a warning indicating that Function...
1
by: fretIT | last post by:
Hello, when I write web method using C# in Visual basic 2005, I can't return the string value to the client request. I got such kind of error Not all code paths return a value. Don't know how...
9
by: reachmsn | last post by:
Hi, At the url http://www.python.org/doc/essays/graphs.html there is some code by Guido Van Rossum for computing paths through a graph - I have pasted it below for reference - Let's write a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.