C# 从If语句返回Int
如果条件为真,我试图使int等于某个值。 我很难从if语句中获取bloodtype int,以便将其应用于我的类。我知道这可能是一个简单的解决办法,但我的大脑已经崩溃了C# 从If语句返回Int,c#,wpf,xaml,if-statement,boolean,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;
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();
}
当您使用if-else-if结构时,仅当所有其他条件都失败时,才会评估您想要血型的位置。此外,在将其传递给Patient的构造函数之前,将其分配给3。因此,在计算此代码时,血型将等于3。只有在使用if-else-if结构时所有其他条件都失败时,才会计算您想要血型的位置。此外,在将其传递给Patient的构造函数之前,将其分配给3。因此,在计算该代码时,血型将等于3
int bloodtype = -1;
(或其他一些您不会使用的值)。变量仅在if else
语句中设置,因此不能将其发送到Patient
类,因为它不等于条件之外的任何内容 试试类似的方法
int bloodtype = -1;
(或其他一些您不会使用的值)。变量仅在
if else
语句中设置,因此不能将其发送到Patient
类,因为它不等于条件之外的任何内容 您需要反转您的血型条件,并从血型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();
}
您需要反转您的血型条件,并从血型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();
}
当它在方法中时,它是方法中的局部变量。如果
血型
等于3,则将其声明为方法之外的字段或属性,并且该字段或属性将仅对您正在创建的类的其余部分可用,并将患者添加到患者
。最后一个else语句应该只包含bloodType=3
,当它在方法中时,它是方法中的局部变量。如果血型
等于3,则将其声明为方法之外的字段或属性,并且该字段或属性将仅对您正在创建的类的其余部分可用,并将患者添加到患者
。最后一条else语句应仅包含bloodType=3