从If语句

时间:2017-03-07 00:46:07

标签: c# wpf xaml if-statement boolean

如果条件为真,我试图使int等于某个值。 我在从if语句中获取bloodtype int时遇到问题,因此可以应用于我的班级。我知道它可能是一个简单的解决方案,但我的大脑是油炸的。

 private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Add Patients
    {
        string name = txtPatientName.Text;
        int bloodType,age=30;           
        DateTime dob;
        bool bloodA = rbA.IsChecked.Equals(true);
        bool bloodB = rbB.IsChecked.Equals(true);
        bool bloodAB = rbAB.IsChecked.Equals(true);
        bool blood0 = rb0.IsChecked.Equals(true);




        // if (dpDOB.SelectedDate == null || txtPatientName.Text == ""||bloodType==0)
        if (dpDOB.SelectedDate == null || txtPatientName.Text == "" || !bloodA || !bloodAB || !bloodB || !blood0)
        {

            if (txtPatientName.Text == "")
            {
                MessageBox.Show("Please enter Patient's Name");
            }

            else if (!bloodA || !bloodAB || !bloodB || !blood0)
            {
                MessageBox.Show("Please enter patient's blood type");
            }

            //else if (dpDOB.SelectedDate == null)
            //{
            //    MessageBox.Show("Please select a date");
            //}

        }

        else
       if (bloodA)
        {
            bloodType = 0;

        }
        else if (bloodB)
        {
            bloodType = 1;
        }
        else if (bloodAB)
        {
            bloodType = 2;
        }
        else {             
            bloodType = 3;           

            dob = dpDOB.SelectedDate.Value;

            Patient patient= new Patient(name, age, bloodType);///cant get bloodtype value

            MainWindow mainWindow = Owner as MainWindow;

           patients.Add(patient);
            lstPatients.ItemsSource = null;
            lstPatients.ItemsSource = patients;
            // this.Close();
        }

3 个答案:

答案 0 :(得分:3)

只有当您使用if else if结构时所有其他条件都失败时,才会评估您想要bloodType的位置。此外,您在将其传递给Patient的构造函数之前将其分配给3。因此,在评估此代码时,bloodType将等于3.

答案 1 :(得分:1)

尝试类似

的内容
int bloodtype = -1;

(或其他一些你不会使用的值)。变量只在if else语句中设置,所以你不能将它发送到Patient类,因为它不等于条件之外的任何东西。

答案 2 :(得分:0)

您需要反转您的血型条件,并从血型3检查区中取出您的患者:

private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Add Patients
{
    string name = txtPatientName.Text;
    int bloodType,age=30;           
    DateTime dob;
    bool bloodA = rbA.IsChecked.Equals(true);
    bool bloodB = rbB.IsChecked.Equals(true);
    bool bloodAB = rbAB.IsChecked.Equals(true);
    bool blood0 = rb0.IsChecked.Equals(true);


    var bloodTypeDefined = bloodA || bloodAB || bloodB || blood0;

    // if (dpDOB.SelectedDate == null || txtPatientName.Text == ""||bloodType==0)
    if (dpDOB.SelectedDate == null || txtPatientName.Text == "" || !bloodTypeDefined)
    {

        if (txtPatientName.Text == "")
        {
            MessageBox.Show("Please enter Patient's Name");
        }

        else if (!bloodTypeDefined)
        {
            MessageBox.Show("Please enter patient's blood type");
        }

        //else if (dpDOB.SelectedDate == null)
        //{
        //    MessageBox.Show("Please select a date");
        //}

    }

    else
    {
       if (bloodA) bloodType = 0;
       else if (bloodB) bloodType = 1;
        else if (bloodAB) bloodType = 2;
        else bloodType = 3;           

        dob = dpDOB.SelectedDate.Value;

        Patient patient= new Patient(name, age, bloodType);///cant get bloodtype value

        MainWindow mainWindow = Owner as MainWindow;

       patients.Add(patient);
        lstPatients.ItemsSource = null;
        lstPatients.ItemsSource = patients;         

    }


        // this.Close();
    }