gpt4 book ai didi

c# - 如果 bool 为真则返回 int

转载 作者:太空宇宙 更新时间:2023-11-03 19:48:23 24 4
gpt4 key购买 nike

根据选中的单选按钮,我需要一个等于特定值的 int 血型。如果任何 bool 为真,它将使血型等于特定值。我只是不知道如何让 bool 为 int 血型赋值。有什么指点吗?

private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Adds Patients using buttone etc to set properties
{
string name = txtPatientName.Text;
int bloodType,x=1;
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 == "" || !bloodA || !bloodAB || !bloodB || !blood0)
{

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

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


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

}

else
{
//bloodType How to make this equal to a value depending on what radio button is checked?
dob = dpDOB.SelectedDate.Value;

Patient patient = new Patient(name, bloodType, x, dob);

MainWindow mainWindow = Owner as MainWindow;

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

最佳答案

首先,我建议提取类型,准确地说是enum:

  // [Flags] // you may want declare enum as Flags
public enum BloodType {
O = 0,
A = 1,
B = 2,
AB = 3
}

然后你可以在三元运算符的帮助下分配BloodType值:

  BloodType bloodType = 
rb0.IsChecked ? BloodType.O
: rbA.IsChecked ? BloodType.A
: rbB.IsChecked ? BloodType.B
: BloodType.AB;

如果你想得到 int 值,只需转换:

 int v = (int) bloodType;

BloodType t = (BloodType) v;

关于c# - 如果 bool 为真则返回 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42657813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com