2017-03-07 139 views
1

我试图使一个int等于某个值,如果条件为真。 我很难从if语句中获取bloodtype int,所以它可以应用于我的课程。我知道它可能是一个简单的解决方案,但我的大脑被炸。从If语句返回Int 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(); 
     } 
+0

当它是在方法的局部变量的方法中。将其声明为方法外部的字段或属性,并且可供类的其他成员使用 – Nkosi

+0

如果'bloodType'等于3,则只会创建患者并将其添加到患者。最后一条else语句包含'bloodType = 3' –

回答

1

尝试像

int bloodtype = -1; 

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

3

只有当所有其他条件都失败时,您才需要评估bloodType的位置,因为您使用if else if结构。此外,您将其分配给3之前传递给患者的构造函数。因此,在这段代码被评估的那一刻,血型将等于3

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(); 
    }