2010-04-06 3 views
1

Je reçois un code inaccessible détecté pour la seconde instruction if. Pouvez-vous s'il vous plaît laissez-moi savoir ce qui a mal tourné?code inaccessible détecté

private bool ValidateSettings() 
{ 
    if (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; 
    } 
} 

Répondre

0
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; 

} 

ce travaille

+0

Mais beaucoup plus désordonné qu'il doit être. –

26

En effet, le premier bloc if/else retournera de toute façon - pas de code après que le bloc exécutera:

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; 
} 
3

Peut-être que vous voulez supprimer les else blocs et juste revenir true à la fin .

On dirait que vous voulez retourner false si l'un des paramètres ne sont pas comme prévu. Laissez:

condition1 = chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text) condition2 = chkpplaORfmp.Checked && String.IsNullOrEmpty(txtfmpORppla.Text)

La façon dont il est écrit, nous avons

  • (condition1, condition2) = (true, true) => return true
  • (condition1, condition2) = (vrai, faux) => renvoyer vrai
  • (condition1, condition2) = (faux, vrai) => return false
  • (condition1, condition2) = (false, false) => return false

Qu'est-ce qu'il ressemble à vous voulez est:

  • (condition1, condition2) = (true, true) = > return true
  • (condition1, condition2) = (true, false) => return false
  • (condition1, condition2) = (false, true) => return false
  • (conditi On1, condition2) = (false, false) => return false
1

Votre code est l'équivalent de ce fait, puisque les deux if et else contiennent des déclarations de retour:

private bool ValidateSettings() 
{ 
    if(chkDownload.Checked && String.IsNullOrEmpty(txtAppName.Text)) 
    { 
     divAppDownloadError.Visible=true; 
     return false; 
    } 

    return true; 
} 
0

Samuel Carrijo est droit; Je pense que ce que vous voulez faire est de vérifier si des conditions invalidantes sont maintenues et, le cas échéant, retourner false. Mais pour les vérifier tout ce que vous ne devez pas retourner true jusqu'à la fin:

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; 
} 
+0

si je retourne la valeur booléenne après vérification condition1 alors état 2 n'est pas vérifié mais je veux évaluer pour la condition 1 et la condition 2 i.e la condition suivante n'est pas satisfaite ((condition1, condition2) = (faux, faux) => retour faux – xrx215

0
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 simplifié

+0

merci mal essayer ceci ... – xrx215

+1

ok m'a frappé avec un vote up ou une réponse sélectionnée si cela fonctionne pour vous –

+3

Bleh Ne donnez pas de poisson, n'enseignez pas à pêcher – Beska

Questions connexes