c# - 从 If 语句返回 Int

标签 c# wpf xaml if-statement boolean

如果条件为真,我试图使一个 int 等于某个值。 我无法从 if 语句中获取血型 int,因此可以将其应用于我的类(class)。我知道这可能是一个简单的解决方案,但我的大脑被炸了。

 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。因此,在评估此代码时,bloodType 将等于 3。

关于c# - 从 If 语句返回 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42638241/

相关文章:

c# - 当作为 IIS7 应用程序发布时,MVC 4 捆绑中断

WPF-是否可以将 OnVerticalOffsetChanged 事件添加到自定义文本框?

wpf - 窗口大小更改时在 WPF 中调用的方法?

xaml - 全局路由当前不能是堆栈中的唯一页面

c# - 为集合 UWP 中的每个项目生成一个组

c# - 连接字符串 Visual Studio 2013 数据库——无法识别的转义序列

java - 将Java SWT/AWT桥嵌入winform形式

c# - 更改 XAML 中的绑定(bind)值

xaml - Windows Phone 8.1 上使用 MVVM Light 的双向数据绑定(bind)

c# - C# 中的字符串格式化以获得相同的间距