运营商'&&'不能应用于'string'和'string'类型的操作数

时间:2016-03-15 11:45:27

标签: c# asp.net .net visual-studio

如何解决包含运算符'&&'的错误不能应用于'string'和'string'类型的操作数。代码可以作为:

bool AttorneysData()
{
    if (((txtFirstName.Text != "")
                && ((txtSurname.Text != "")
                && (txtAddress.Text
                && (txtTelNo.Text
                && (txtEmail.Text
                && (txtCreatePassword.Text
                && (txtConfirmPassword.Text
                && (cboDisciplineExpertise.Text
                && (cboGeographicalLocation.Text
                && (cboBudgetEstimation.Text
                && (txtNoofIndividuals.Text
                && (cboTypeofClients.Text
                && (txtCompanyName.Text && txtYearsofExperience.Text))))))))))))))
    {
        return true;
    }
    else
    {
        return false;
    }

}

2 个答案:

答案 0 :(得分:3)

if语句的每个术语(term1 && term2 && ...)必须解析为布尔值(true / false)

你可以这样解决:

bool AttorneysData()
{
   if (txtFirstName.Text != ""
            && txtSurname.Text != ""
            && txtAddress.Text != ""
            && txtTelNo.Text != ""
            && txtEmail.Text != ""
            && txtCreatePassword.Text != ""
            && txtConfirmPassword.Text != ""
            && cboDisciplineExpertise.Text != ""
            && cboGeographicalLocation.Text != ""
            && cboBudgetEstimation.Text != ""
            && txtNoofIndividuals.Text != ""
            && cboTypeofClients.Text != ""
            && txtCompanyName.Text  != ""
            && txtYearsofExperience.Text != "")
   {
       return true;
   }
   else
   {
       return false;
   }
}

或者稍微优雅一点:

return txtFirstName.Text != ""
    && txtSurname.Text != ""
    && txtAddress.Text != ""
    && txtTelNo.Text != ""
    && txtEmail.Text != ""
    && txtCreatePassword.Text != ""
    && txtConfirmPassword.Text != ""
    && cboDisciplineExpertise.Text != ""
    && cboGeographicalLocation.Text != ""
    && cboBudgetEstimation.Text != ""
    && txtNoofIndividuals.Text != ""
    && cboTypeofClients.Text != ""
    && txtCompanyName.Text  != ""
    && txtYearsofExperience.Text != "";

另外,请考虑使用string.IsNullOrEmpty method来测试空格和其他空字符:

return !string.IsNullOrEmpty(txtSurname.Text) 
    && !string.IsNullOrEmpty(txtAddress.Text) 
    ...

答案 1 :(得分:2)

每次都需要使用逻辑运算符。如果你想将所有字符串变为不等式以清空字符串,那么使用String.IsNullOrEmpty()方法而不是!=运算符。

此外,没有理由在表达式中使用()。您需要使用括号来确定操作的优先级,但在您的代码中没有优先级冲突,因为所有操作都可以从左到右连续执行。

您的if语句已包含返回布尔结果的逻辑表达式,然后您可以将其与返回运算符交换。

return String.IsNullOrEmpty(txtFirstName.Text)
            && String.IsNullOrEmpty(txtSurname.Text)
            && String.IsNullOrEmpty(txtAddress.Text)
            && String.IsNullOrEmpty(txtTelNo.Text)
            && String.IsNullOrEmpty(txtEmail.Text)
            && String.IsNullOrEmpty(txtCreatePassword.Text)
            && String.IsNullOrEmpty(txtConfirmPassword.Text)
            && String.IsNullOrEmpty(cboDisciplineExpertise.Text)
            && String.IsNullOrEmpty(cboGeographicalLocation.Text)
            && String.IsNullOrEmpty(cboBudgetEstimation.Text)
            && String.IsNullOrEmpty(txtNoofIndividuals.Text)
            && String.IsNullOrEmpty(cboTypeofClients.Text)
            && String.IsNullOrEmpty(txtCompanyName.Text) 
            && String.IsNullOrEmpty(txtYearsofExperience.Text);