2017-03-07 80 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(); 
    }