Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么字符串比较会返回意外结果?_C#_String_Winforms - Fatal编程技术网

C# 为什么字符串比较会返回意外结果?

C# 为什么字符串比较会返回意外结果?,c#,string,winforms,C#,String,Winforms,我正在尝试创建一个带有添加按钮和一些信息文本框的表单。当我点击add按钮时,文本框中给出的所有信息都应该被读取并显示在列表视图和树状视图中。这是我的添加按钮点击事件代码 private void btnAdd_Click(object sender, EventArgs e) { if(txtBYear.Text == "" || txtGender.Text == "" || txtMSSV.Text == "" || txtName.Text=="" ||

我正在尝试创建一个带有添加按钮和一些信息文本框的表单。当我点击add按钮时,文本框中给出的所有信息都应该被读取并显示在列表视图和树状视图中。这是我的添加按钮点击事件代码

private void btnAdd_Click(object sender, EventArgs e)
{
    if(txtBYear.Text == "" ||
       txtGender.Text == "" || txtMSSV.Text == "" ||
       txtName.Text=="" || cBoxClasses.Text == "")
    {
        MessageBox.Show("Missing information!", "Error");
        return;
    }

    if (txtGender.Text !="Female" || txtGender.Text!="Male")
    {
        MessageBox.Show("Male or Female only!", "Error");
        return;
    }

    var mssv = txtMSSV.Text;
    var name = txtName.Text;

    uint bYear;
    bool gender;

    var addr = txtAddress.Text;
    uint cl = (uint)cBoxClasses.SelectedValue;
    var clName = cBoxClasses.SelectedText;

    if (txtGender.Text == "Female")
        gender = true;
    else 
        gender = false;

    if (uint.TryParse(txtBYear.Text, out bYear))
    {
        MessageBox.Show("Incorrect Birth Year!", "Error");
        return;
    }

    var newStudent = new Student(cl, mssv, name, bYear, gender, addr);

    Classes x;

    if(classes.GetClassesById(cl, out x))
    {
        x.AttendingStudents.Add(newStudent);
    }
    else
    {
        var tmp = new Classes(cl, clName);
        classes.Add(tmp);
        tmp.AttendingStudents.Add(newStudent);
    }

    lViewMain.Update();
    tViewMain.Update();
}
我尝试在txtGender文本框中键入“Female”并执行按钮单击,但我的比较结果返回
true
,从而弹出
Messagebox


为什么我的支票
textGender.Text!=“女性”| | txtGender.Text!=“Male”
返回该意外值?

运算符表示“or”,如果其中一条语句为true,则返回true

textGender.Text!=“女性”
返回
false

txtGender.Text!=“男性”
返回
true


这意味着
textGender.Text!=“女性”| | txtGender.Text!=“Male”
返回
true

代码执行您让它执行的操作。您的状况
textGender.Text!=“女性”| | txtGender.Text!=“男性”
表示“如果文本不是‘女性’或文本不是‘男性’

因为它不是'Male',所以它进入if块。您需要的是
&&
操作符

请尝试使用以下条件

textGender.Text!=“女性”和&txtGender.Text!=“男性”

应该是

if (txtGender.Text !="Female" && txtGender.Text!="Male")
{
   MessageBox.Show("Male or Female only!", "Error");
   return;
}
因为,当文本不是女性也不是男性时,您会抛出一个错误,如果是其中一个,就让它通过

 textGender.Text != "Female" ||txtGender.Text != "Male" 
如果
txtGender.Text
“女性”
,则为true,因为
txtGender.Text!=“男性”
变为true

你想要的是

 !(textGender.Text == "Female" ||txtGender.Text == "Male")


“只有男性或女性!“当您输入“女性”时,文本不是“男性”,因此您的条件为真。您的条件永远不会为假!顺便说一句,您的代码有很多可能的改进,从比较字符串的方式(例如查找
string.IsNullOrWhitespace
)或理解布尔值开始:
bool-gender;if(txtgenerd.text==”Female“)gender=true;else gender=false;
可以重写为
bool gender=txtGender.Text==“Female”
,当然您应该使用
enum
进行类似操作。
 textGender.Text != "Female" && txtGender.Text != "Male"