如何检查C#中是否设置了类属性?

如何检查C#中是否设置了类属性?,c#,C#,我目前正在为我的C#班做一个体重指数计算器。我的应用程序要求用户以英尺和英寸表示身高,以磅表示体重,或者以厘米表示身高,以千克表示体重。它们是一个选项卡控件,具有英制模式或公制模式的按钮 如果单击imperial选项卡中的“计算”按钮,我需要确保已填写heightFt、heightIn和weightLbs文本框。然后我把它们转换成公制单位。如果单击“公制”选项卡中的“计算”按钮,我需要确保已填写“高度cm”和“重量kg”文本框 一旦提供了这些数据,我就会将身高和体重转换为BMI。如果我使用If(

我目前正在为我的C#班做一个体重指数计算器。我的应用程序要求用户以英尺和英寸表示身高,以磅表示体重,或者以厘米表示身高,以千克表示体重。它们是一个选项卡控件,具有英制模式或公制模式的按钮

如果单击imperial选项卡中的“计算”按钮,我需要确保已填写heightFt、heightIn和weightLbs文本框。然后我把它们转换成公制单位。如果单击“公制”选项卡中的“计算”按钮,我需要确保已填写“高度cm”和“重量kg”文本框

一旦提供了这些数据,我就会将身高和体重转换为BMI。如果我使用
If(heightFt==null)
我会得到以下错误:

The result of the expression is always 'false' since a value of type 'double' is never equal to 'null' of type 'double?'
我该如何解决这个问题

这是我的MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BMICalculator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private BMICalc _BMICalc;

        public MainWindow()
        {
            _BMICalc = new BMICalc();
            InitializeComponent();
        }

        private void calculateImperial_Click(object sender, RoutedEventArgs e)
        {

            _BMICalc.heightFt = Convert.ToDouble(heightFt.Text);
            _BMICalc.heightIn = Convert.ToDouble(heightIn.Text);
            _BMICalc.weightLbs = Convert.ToDouble(weightLbs.Text);

            _BMICalc.doCalculation("Imperial");

            if (_BMICalc.error == null)
            {

                MessageBox.Show("Your BMI is " + _BMICalc.bmi + " and you are " + _BMICalc.category, "Your BMI");

            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间BMI计算器
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
私人BMICalc _BMICalc;
公共主窗口()
{
_BMICalc=新的BMICalc();
初始化组件();
}
私有void calculateImperial_Click(对象发送者,路由目标)
{
_BMICalc.heightFt=Convert.ToDouble(heightFt.Text);
_BMICalc.heightIn=Convert.ToDouble(heightIn.Text);
_BMICalc.weightLbs=Convert.ToDouble(weightLbs.Text);
_bmical.doccalculation(“英制”);
如果(_BMICalc.error==null)
{
MessageBox.Show(“你的BMI是”+_BMICalc.BMI+”而你是”+_BMICalc.category,“你的BMI”);
}
}
}
}
这里是BMICalc.cs

using System;
using System.Windows;
using System.ComponentModel;

namespace BMICalculator
{
    class BMICalc :INotifyPropertyChanged
    {
        public double _heightFt { get; set; }
        public double _heightIn { get; set; }
        public double _heightCm { get; set; }
        public double _weightLbs { get; set; }
        public double _weightKg { get; set; }
        public double _bmi { get; set; }
        public string _category { get; set; }
        public string _error { get; set; }

        public double heightFt
        {

            get { return heightFt; }
            set
            {
                _heightFt = value;
                OnPropertyChanged("heightFt");
            }

        }

        public double heightIn
        {

            get { return _heightIn; }
            set
            {
                _heightIn = value;
                OnPropertyChanged("heightIn");
            }

        }

        public double heightCm
        {
            get { return _heightCm; }
            set
            {
                _heightCm = value;
                OnPropertyChanged("heightCm");
            }

        }

        public double weightLbs
        {

            get { return _weightLbs; }
            set
            {
                _weightLbs = value;
                OnPropertyChanged("weightLbs");
            }

        }

        public double weightKg
        {

            get { return _weightKg; }
            set
            {
                _weightKg = value;
                OnPropertyChanged("weightKg");
            }

        }

        public double bmi
        {
            get { return _bmi; }
            set
            {
                _bmi = value;
                OnPropertyChanged("bmi");
            }
        }

        public string error
        {
            get { return _error; }
            set
            {
                _error = value;
                OnPropertyChanged("error");
            }
        }

        public string category
        {
            get { return _category; }
            set
            {
                _category = value;
                OnPropertyChanged("category");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName){

            if(PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

        public void doCalculation(string calculationMode){
            if(calculationMode == "Imperial"){

                if (heightFt == null)
                {
                    this.error = "You must provide a value for your height in feet!";
                }
                else if (heightIn == null)
                {
                    this.error = "You must provide a value for your height in inches!";
                }
                else if (weightLbs == null)
                {
                    this.error = "You must provide a value for your weight in pounds!";
                }
                else
                {
                    heightFt = Convert.ToDouble(heightFt);
                    heightIn = Convert.ToDouble(heightIn);
                    weightLbs = Convert.ToDouble(weightLbs);

                    _weightKg = Convert.ToDouble(_weightLbs * (1 / 2.2));
                    _heightCm = Convert.ToDouble((_heightFt + (_heightIn / 12) / 0.032808));
                }

            } else if(calculationMode == "Metric"){

                    this.bmi = weightKg / Math.Pow((heightCm / 100), 2);

                    if (this.bmi < 18.5)
                    {
                        this.category = "underweight";
                    }
                    else if (this.bmi >= 18.5 && this.bmi < 24.9)
                    {
                        this.category = "normalweight";
                    }
                    else if (this.bmi >= 25 && this.bmi <= 29.9)
                    {
                        this.category = "overweight";
                    }
                    else if (this.bmi > 30)
                    {
                        this.category = "obese";
                    }

                }

            }
        }
    }
}
使用系统;
使用System.Windows;
使用系统组件模型;
名称空间BMI计算器
{
类BMICalc:INotifyPropertyChanged
{
公共双高度{get;set;}
公共双_heightIn{get;set;}
公共双_heightCm{get;set;}
公共双重{get;set;}
公共双重{get;set;}
公共双_bmi{get;set;}
公共字符串_类别{get;set;}
公共字符串_错误{get;set;}
公共双高度
{
获取{return heightFt;}
设置
{
_高度ft=数值;
不动产变更(“高度”);
}
}
公共双高度
{
获取{return\u heightIn;}
设置
{
_高度=值;
不动产变更(“heightIn”);
}
}
公共双高
{
获取{return\u heightCm;}
设置
{
_高度cm=数值;
不动产变更(“高度CM”);
}
}
公共双重磅
{
获取{return\u weightLbs;}
设置
{
_权重Lbs=值;
不动产变更(“重量磅”);
}
}
公众双倍体重公斤
{
获取{return\u weightKg;}
设置
{
_重量kg=数值;
不动产变更(“重量kg”);
}
}
公共双倍体重指数
{
获取{return\u bmi;}
设置
{
_bmi=数值;
不动产变更(“bmi”);
}
}
公共字符串错误
{
获取{return\u error;}
设置
{
_误差=数值;
OnPropertyChanged(“错误”);
}
}
公共字符串类别
{
获取{return\u category;}
设置
{
_类别=价值;
不动产变更(“类别”);
}
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName){
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共无效数据计算(字符串计算模式){
如果(计算模式==“英制”){
如果(heightFt==null)
{
this.error=“您必须提供以英尺为单位的高度值!”;
}
else if(heightIn==null)
{
this.error=“您必须提供一个以英寸为单位的高度值!”;
}
else if(weightLbs==null)
{
this.error=“您必须提供以磅为单位的体重值!”;
}
其他的
{
heightFt=转换为双精度(heightFt);
heightIn=Convert.ToDouble(heightIn);
weightLbs=转换为双倍(weightLbs);
_重量kg=换算成双倍(_-weightLbs*(1/2.2));
_heightCm=转换为双((_heightFt+(_heightIn/12)/0.032808));
}
}else if(calculationMode==“公制”){
此bmi=体重kg/数学功率((身高cm/100),2);
如果(该体重指数<18.5)
{
this.category=“体重不足”;
}
否则如果(this.bmi>=18.5&&this.bmi<24.9)
{
这个类别=
public double? heightIn
{
    get { return _heightIn; }
    set {
        _heightIn = value;
        OnPropertyChanged("heightIn");
    }
}
class BMICalc :INotifyPropertyChanged
    {
        private double _heightFt = 0;
        private double _heightIn = 0;
        private double _heightCm = 0;
        private double _weightLbs = 0;
        private double _weightKg = 0;
        private double _bmi = 0;
        private string _category = null;
        private string _error = null;

        public double heightFt
        {

            get { return _heightFt; }
            set
            {
                _heightFt = value;
                OnPropertyChanged("heightFt");
            }

        }

        public double heightIn
        {

            get { return _heightIn; }
            set
            {
                _heightIn = value;
                OnPropertyChanged("heightIn");
            }

        }

        public double heightCm
        {
            get { return _heightCm; }
            set
            {
                _heightCm = value;
                OnPropertyChanged("heightCm");
            }

        }

        public double weightLbs
        {

            get { return _weightLbs; }
            set
            {
                _weightLbs = value;
                OnPropertyChanged("weightLbs");
            }

        }

        public double weightKg
        {

            get { return _weightKg; }
            set
            {
                _weightKg = value;
                OnPropertyChanged("weightKg");
            }

        }

        public double bmi
        {
            get { return _bmi; }
            set
            {
                _bmi = value;
                OnPropertyChanged("bmi");
            }
        }

        public string error
        {
            get { return _error; }
            set
            {
                _error = value;
                OnPropertyChanged("error");
            }
        }

        public string category
        {
            get { return _category; }
            set
            {
                _category = value;
                OnPropertyChanged("category");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName){

            if(PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

        public void doCalculation(string calculationMode){
            if(calculationMode == "Imperial"){

                if (heightFt == null)
                {
                    this.error = "You must provide a value for your height in feet!";
                }
                else if (heightIn == null)
                {
                    this.error = "You must provide a value for your height in inches!";
                }
                else if (weightLbs == null)
                {
                    this.error = "You must provide a value for your weight in pounds!";
                }
                else
                {
                    heightFt = Convert.ToDouble(heightFt);
                    heightIn = Convert.ToDouble(heightIn);
                    weightLbs = Convert.ToDouble(weightLbs);

                    _weightKg = Convert.ToDouble(_weightLbs * (1 / 2.2));
                    _heightCm = Convert.ToDouble((_heightFt + (_heightIn / 12) / 0.032808));
                }

            } else if(calculationMode == "Metric"){

                    this.bmi = weightKg / Math.Pow((heightCm / 100), 2);

                    if (this.bmi < 18.5)
                    {
                        this.category = "underweight";
                    }
                    else if (this.bmi >= 18.5 && this.bmi < 24.9)
                    {
                        this.category = "normalweight";
                    }
                    else if (this.bmi >= 25 && this.bmi <= 29.9)
                    {
                        this.category = "overweight";
                    }
                    else if (this.bmi > 30)
                    {
                        this.category = "obese";
                    }

                }

            }
        }
    }