简体   繁体   中英

How can I check if a class property is set in C#?

I am currently working on a BMI calculator for my C# class. My app asks the user for either their height in feet and inches and their weight in pounds or their height in centimeters and weight in kilograms. Their is a tab control that has buttons for Imperial mode or Metric mode.

If the "calculate" button in the imperial tab is clicked, I need to make sure that the heightFt, heightIn, and weightLbs text boxes have been filled out. Then I convert those to the metric units. If the calculate button in the metric tab is clicked, I need to make sure that the heightCm and weightKg text boxes have been filled out.

Once those are provided, I then convert the height and weight to BMI. If I use if(heightFt == null) I get the following error:

The result of the expression is always 'false' since a value of type 'double' is never equal to 'null' of type 'double?'

How do I fix this?

This is my 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");

            }

        }
    }
}

This is 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";
                    }

                }

            }
        }
    }
}

You can make you double properties to be nullable so you can compare to null (the default).

Example:

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

In this case, make sure you change the type of the corresponding private variables to match.

You never set a default value for your properties. As double cannot be null you'll probably want to check against 0 instead. The properties you begin with underscores should just be private variables with default values. Also consider using metric only internally.

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";
                    }

                }

            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM