Not all code paths return a value [c#]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    Not all code paths return a value [c#]

    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:

    Code:
    private bool IPExist(string IP)
            {
                // We need to check if the IP exists in the
                // IP array
                foreach (string x in IPs)
                {
                    if (x == IP)
                    {
                        // IP exists
                        return false;
                    }
                    else return true;
                }
            }
    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?
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    You also need to return true or false outside of your foreach loop.

    Nathan

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      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.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by nateraaaa
        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.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by insertAlias
          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)
          Code:
          private bool IPExist(string IP)
                  {
                      // We need to check if the IP exists in the
                      // IP array
                      foreach (string x in IPs)
                      {
                          if (x == IP)
                          {
                              // IP exists
                              return true;
                          }
                      }
                      return false;
                  }
          and it works.

          Comment

          Working...