【发布时间】:2017-07-27 01:37:17
【问题描述】:
如果条件为真,我正在尝试使 int 等于某个值。 我无法从 if 语句中获取血型 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();
}
【问题讨论】:
-
当它在方法中时,它是方法中的局部变量。将其声明为方法之外的字段或属性,它将可供类的其余部分使用
-
如果
bloodType等于 3,您只是在创建并添加一个患者到patients。最后一个 else 语句应该只包含bloodType = 3
标签: c# wpf xaml if-statement boolean