Skip to content

All paths do not return value help

Daniel

Asked Jun 22, 2007, 10:59 AM EDT3 answers
Share
LinkedInX
Please help. I am getting the error "all paths do not return a value" I can't find it. THANKS IN ADVANCE FOR ANY SUGGESTIONS: HERE'S THE CODE:

private CheckBox FindCheckBox(string sCtrlName)
{
foreach (CheckBox chkTemp in this.Controls)
{
//CheckBox chkTemp = new CheckBox();

//if str is greater than -1, then string was found
if (sCtrlName.IndexOf("YES") > -1)
{
//since YES is selected, find and return
//the "NO" control
int endPos = sCtrlName.IndexOf("_");
string sName = sCtrlName.Substring(0, endPos);

sName += "_NO";

if (chkTemp.Name == sName)
{
//chkTemp = (CheckBox)c;
return chkTemp;
}
}
else
{
//since NO is selected, find and return
//the "YES" control
int endPos = sCtrlName.IndexOf("_");
string sName = sCtrlName.Substring(0, endPos);

sName += "_YES";

if (chkTemp.Name == sName)
{
//chkTemp = (CheckBox)c;
return chkTemp;
}
return chkTemp;
}
}
}

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0

    I found the case, but you should consider having a return at the very end of your loop if none of the other conditions are met. (like return null;)  In any case, it looks like this code could use a little refactoring.  Once you find yourself adding returns all over a method, you need to rethink how the method is written <g>


    private CheckBox FindCheckBox(string sCtrlName)
     {
       foreach (CheckBox chkTemp in this.Controls)
        {
         //CheckBox chkTemp = new CheckBox();
         //if str is greater than -1, then string was found
         if (sCtrlName.IndexOf("YES") > -1)
          {
               //since YES is selected, find and return
               //the "NO" control
               int endPos = sCtrlName.IndexOf("_");
               string sName = sCtrlName.Substring(0, endPos);
               sName += "_NO";
    // if   this if never becomes true, there is no return;
               if (chkTemp.Name == sName)
                  {
                      //chkTemp = (CheckBox)c;
                      return chkTemp;
                  }
                        ********* this is the spot.  ********
             }
            else
            {
              //since NO is selected, find and return
             //the "YES" control
              int endPos = sCtrlName.IndexOf("_");
              string sName = sCtrlName.Substring(0, endPos);
              sName += "_YES";
              if (chkTemp.Name == sName)
                {
                  //chkTemp = (CheckBox)c;
                  return chkTemp;
                }

              return chkTemp;
            }  // end else

         }  // end foreach
        // ******  you should put a return here ****
         return null;

     }  // end method

    0
  • 0
    Thank you for the quick response!!! That would mean it did not find the control. What should I return in that case?? Also, What is the best way to paste formatted code in the forums. I have to insert
    tags after each line. THANKS AGAIN!
    0
  • Mike Gold
    0
    Hi Daniel

    I re-edited my response, so please take a look.  I just pasted the code out of Visual Studio 2005 and it seemed to format okay.  I have had cases, though where it doesn't format.  I think the new forums coming out soon should alleviate some of these problems.  Thanks for the input, though.

    Best,

    -Mike
    0