2021年3月13日星期六

How to get rid of this error: not all code paths return a value

Here is my coding project. I'm almost done, but need help with this error. I've been getting the error "not all code paths return a value" and I don't understand why this is happening?

using System;  using Windows.UI.Xaml;  using Windows.UI.Xaml.Controls;    // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409    namespace Lab4  {      /// <summary>      ///       /// </summary>      public sealed partial class MainPage : Page      {          // variables          string kgs = "kg";          string meters = "m";          string cms = "cm";          string nonApplicable = "n/a";          string bmi = "bmi";          string mhr = "beats / minute";          string bmr = "calories";            public MainPage() => this.InitializeComponent();            // when anyone of the formulas are selected, change the unit to the proper one, then enable/disable and empty certain inputs          private void cboFormulas_SelectionChanged(object sender, SelectionChangedEventArgs e)          {              if (cboFormulas.SelectedIndex == 0)              {                  txtWeightUnit.Text = kgs;                  txtHeightUnit.Text = meters;                  txtformulaUnit.Text = bmi;                    txtAge.Text = String.Empty;                  txtFormulaCalc.Text = String.Empty;                    radGenderM.IsEnabled = false;                  radGenderF.IsEnabled = false;                  txtAge.IsEnabled = false;                  txtWeight.IsEnabled = true;                  txtHeight.IsEnabled = true;              }                if (cboFormulas.SelectedIndex == 1)              {                  txtWeightUnit.Text = nonApplicable;                  txtHeightUnit.Text = nonApplicable;                  txtformulaUnit.Text = mhr;                    radGenderF.IsChecked = false;                  radGenderM.IsChecked = false;                  txtWeight.Text = String.Empty;                  txtHeight.Text = String.Empty;                  txtFormulaCalc.Text = String.Empty;                                      radGenderM.IsEnabled = false;                  radGenderF.IsEnabled = false;                  txtAge.IsEnabled = true;                  txtWeight.IsEnabled = false;                  txtHeight.IsEnabled = false;              }                else if (cboFormulas.SelectedIndex == 2)              {                  txtWeightUnit.Text = kgs;                  txtHeightUnit.Text = cms;                  txtformulaUnit.Text = bmr;                    txtFormulaCalc.Text = String.Empty;                    radGenderM.IsEnabled = true;                  radGenderF.IsEnabled = true;                  txtAge.IsEnabled = true;                  txtWeight.IsEnabled = true;                  txtHeight.IsEnabled = true;              }          }            // if button is clicked, check which formula is selected, perform the calculation for said formula, and show the final answer          private void btnCalculate_Click(object sender, RoutedEventArgs e)          {              if (cboFormulas.SelectedIndex == 0)              {                  double inputWeight = Convert.ToDouble(txtWeight.Text);                  double inputHeight = Convert.ToDouble(txtHeight.Text);                  double bmiResult = bmiFormula(inputWeight, inputHeight);                    txtFormulaCalc.Text = Convert.ToString(bmiResult);              }                else if (cboFormulas.SelectedIndex == 1)              {                  double inputAge = Convert.ToDouble(txtAge.Text);                  double mhrResult = mhrFormula(inputAge);                    txtFormulaCalc.Text = Convert.ToString(mhrResult);              }                else if (cboFormulas.SelectedIndex == 2)              {                  double inputAge = Convert.ToDouble(txtAge.Text);                  double inputWeight = Convert.ToDouble(txtWeight.Text);                  double inputHeight = Convert.ToDouble(txtHeight.Text);                  double bmiResult = bmrFormula(inputAge, inputWeight, inputHeight);                    txtFormulaCalc.Text = Convert.ToString(bmiResult);                  //txtFormulaCalc.Text = Conve;              }          }            // BMI formula          private double bmiFormula(double weight, double height)          {              double bmiSum = weight / Math.Pow(height, 2);              return bmiSum;          }            // MHR formula          private double mhrFormula(double age)          {              double mhrSum = 208 - (0.7 * age);              return mhrSum;          }            // BMR formula          private double bmrFormula(double age, double weight, double height)          {              // perform male bmr forumla              if (radGenderM.IsChecked == true)              {                  double bmrSum = (66.47 + (13.75 * weight) + (5 * height) - (6.75 * age));                  return bmrSum;              }                // perform female bmr formula              else if (radGenderF.IsChecked == true)              {                  double bmrSum = (665.09 + (9.56 * weight) + (1.84 * height) - (4.67 * age));                  return bmrSum;              }          }      }  }  

This is where I seem to be getting the error which I don't understand why?

private double bmrFormula(double age, double weight, double height)          {              // perform male bmr forumla              if (radGenderM.IsChecked == true)              {                  double bmrSum = (66.47 + (13.75 * weight) + (5 * height) - (6.75 * age));                  return bmrSum;              }                // perform female bmr formula              else if (radGenderF.IsChecked == true)              {                  double bmrSum = (665.09 + (9.56 * weight) + (1.84 * height) - (4.67 * age));                  return bmrSum;              }          }      }  }  

I've tried multiple times trying to fix it, but I've run out of ideas. Any help with this problem would be much appreciated, but please explain how to how to fix it and not just the answer.

https://stackoverflow.com/questions/66620984/how-to-get-rid-of-this-error-not-all-code-paths-return-a-value March 14, 2021 at 12:04PM

没有评论:

发表评论