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;
}
}
}
All paths do not return value help
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mike GoldRank #50 · 30.6K pointsPosted Jun 22, 2007, 11:17 AM EDT
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
Daniel58 pointsPosted Jun 22, 2007, 11:22 AM EDT
tags after each line. THANKS AGAIN!
Mike GoldRank #50 · 30.6K pointsPosted Jun 22, 2007, 11:24 AM EDT
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