469,360 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

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 9880
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

Post your reply

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

Similar topics

1 post views Thread by bdinmstig | last post: by
6 posts views Thread by Bruce W.1 | last post: by
4 posts views Thread by OutdoorGuy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.