unreachable code detected
I am getting unreachable code detected for the second if statement. Can you please let me know what went wrong?
private bool ValidateSettings()
{
if (开发者_C百科chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text))
{
divAppDownloadError.Visible=true;
return false;
}
else
{
return true;
}
if (chkpplaORfmp.Checked && String.IsNullOrEmpty(txtfmpORppla.Text))
{
divXPAAPPDownloadError.Visible = true;
return false;
}
else
{
return true;
}
}
This is because the first if/else
block will return either way - no code after that block will execute:
if(chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text))
{
// You either return here
divAppDownloadError.Visible=true;
return false;
}
else
{
// or here - after this statement how can anything
// else possible execute?
return true;
}
Perhaps you want to remove the else
blocks and just return true
at the end.
Looks like you want to return false
if any of the settings are not as expected. Let:
condition1 = chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text)
condition2 = chkpplaORfmp.Checked && String.IsNullOrEmpty(txtfmpORppla.Text)
The way it is written, we have
- (condition1, condition2) = (true, true) => return true
- (condition1, condition2) = (true, false) => return true
- (condition1, condition2) = (false, true) => return false
- (condition1, condition2) = (false, false) => return false
What it looks like you want is:
- (condition1, condition2) = (true, true) => return true
- (condition1, condition2) = (true, false) => return false
- (condition1, condition2) = (false, true) => return false
- (condition1, condition2) = (false, false) => return false
Your code is the equivalent of this, since both the if
and else
contain return statements:
private bool ValidateSettings()
{
if(chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text))
{
divAppDownloadError.Visible=true;
return false;
}
return true;
}
Samuel Carrijo is right; I think what you mean to do is check if any invalidating conditions are held and, if so, return false
. But to check them all you must not return true
until the end:
private bool ValidateSettings()
{
if (chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text))
{
divAppDownloadError.Visible=true;
return false;
}
if (chkpplaORfmp.Checked && String.IsNullOrEmpty(txtfmpORppla.Text))
{
divXPAAPPDownloadError.Visible = true;
return false;
}
// if you've gotten this far, neither of the
// invalidating conditions above were held;
// so you're good!
return true;
}
private bool ValidateSettings()
{
if ((chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text))||
(chkpplaORfmp.Checked && String.IsNullOrEmpty(txtfmpORppla.Text)))
{
if (chkDownload.Checked)
divAppDownloadError.Visible=true;
else divXPAAPPDownloadError.Visible = true;
return false;
}
return true;
}
Code simplified
private bool ValidateSettings()
{
if (chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text) && chkpplaORfmp.Checked && String.IsNullOrEmpty(txtfmpORppla.Text))
{
divAppDownloadError.Visible = true;
divXPAAPPDownloadError.Visible = true;
return false;
}
if ((chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text)) || (chkpplaORfmp.Checked && String.IsNullOrEmpty(txtfmpORppla.Text)))
{
if (chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text))
{
divAppDownloadError.Visible = true;
divXPAAPPDownloadError.Visible = false;
}
else
{
divXPAAPPDownloadError.Visible = true;
divAppDownloadError.Visible = false;
}
return false;
} return true;
}
this is working
精彩评论