Dean Smylie
Dean Smylie

Reputation: 3

Not Able to output to Label (C#)

I am trying to output data to a label but it is not outputting.

The data that I am outputting is a Double that is to be converted to a string. This is my code so far.`

private void button1_Click(object sender, EventArgs e)
{
    if (cboGender.SelectedText == "Male")
    {
        dblAge = Convert.ToDouble(txtAge.Text);
        dblWeight = Convert.ToDouble(txtWeight);
        dblHeight = Convert.ToDouble(txtHeight);

        // BMR Calculation
        dblBmrResult = 4.536 * dblWeight + 15.88 * dblHeight - 5 * dblAge + 5;
        strBmrResultConvert = Convert.ToString(dblBmrResult);

        //Output
        lblBmrResult.Text = dblBmrResult.ToString();
    }
    else if (cboGender.SelectedText == "Female")
    {
        dblAge = Convert.ToDouble(txtAge.Text);
        dblWeight = Convert.ToDouble(txtWeight);
        dblHeight = Convert.ToDouble(txtHeight);

        // BMR Calculation
        dblBmrResult = 4.536 * dblWeight + 15.88 * dblHeight - 5 * dblAge + 5;
        strBmrResultConvert = Convert.ToString(dblBmrResult);

        //Output
        lblBmrResult.Text = dblBmrResult.ToString();
    }
}

Everything looks fine. Any help to show what I am missing or when wrong will be much appreciated.

Upvotes: 0

Views: 117

Answers (1)

user10216583
user10216583

Reputation:

Honestly, this is more about understanding the Basal Metabolic Rate (BMR) thing that mentioned in the question. I believe the valuable comments up there were enough to solve the main problem of the OP.

How about creating a helper class for that BMR that contains a static function to do the required calculation according to the passed arguments, and another static function to calculate the daily calorie requirement:

public sealed class BmrHelper
{
    #region enums

    public enum Gender
    {
        Female,
        Male,
    }

    public enum CalcUnit
    {
        Imperial,
        Metric,
    }

    public enum ActivityLevel 
    {
        Sedentary,
        LightlyActive,
        ModeratelyActive,
        VeryActive,
        SuperActive,
    }

    #endregion

    #region Constructors

    private BmrHelper() { }

    #endregion

    #region Methods

    //Basal Metabolic Rate
    public static double BMR(
        Gender gender, 
        int age, 
        double weight, 
        double height, 
        CalcUnit calcUnit = CalcUnit.Imperial
        )
    {
        var weightFactor = 0D;
        var heightFactor = 0D;
        var bmr = 0D;

        switch (calcUnit)
        {
            case CalcUnit.Metric:
                weightFactor = 10D;
                heightFactor = 6.25D;
                break;
            case CalcUnit.Imperial:
                weightFactor = 4.536D;
                heightFactor = 15.88D;
                break;
        }

        switch(gender)
        {
            case Gender.Female:
                bmr = (weightFactor * weight) + (heightFactor * height) - (5 * age) - 161;
                break;
            case Gender.Male:
                bmr = (weightFactor * weight) + (heightFactor * height) - (5 * age) + 5;
                break;
        }

        return bmr;
    }

    //Calories Per Day
    public static double CPD(
        Gender gender, 
        int age, 
        double weight, 
        double height,
        ActivityLevel activityLevel,
        CalcUnit calcUnit = CalcUnit.Imperial
        )
    {
        var bmr = BMR(gender, age, weight, height, calcUnit);

        return CPD(bmr, activityLevel);
    }

    public static double CPD(double bmr, ActivityLevel activityLevel)
    {
        var alv = 0D;

        switch(activityLevel)
        {
            case ActivityLevel.Sedentary:
                alv = 1.2D;
                break;
            case ActivityLevel.LightlyActive:
                alv = 1.375D;
                break;
            case ActivityLevel.ModeratelyActive:
                alv = 1.55D;
                break;
            case ActivityLevel.VeryActive:
                alv = 1.725D;
                break;
            case ActivityLevel.SuperActive:
                alv = 1.9D;
                break;
        }

        return bmr * alv;
    }

    #endregion
}

In your implementation, assuming you have a gender combo box with Female and Male items respectively, text boxes for age, weight, and height inputs. A label to show the result:

Also, you may want to add one more combo box to pass the unit of the factors. Imperial and Metric items respectively. If you want to calculate the daily calorie requirement, then you need another combo box to list the ActivityLevels in the same order of the enum.

private void button1_Click(object sender, EventArgs e)
{
    //Validate the inputs

    if (cboGender.SelectedIndex == -1) return;

    if (!int.TryParse(txtAge.Text, out int age))
    {
        MessageBox.Show("Error: Age...");
        return;
    }


    if (!double.TryParse(txtWeight.Text, out double weight))
    {
        MessageBox.Show("Error: Weight...");
        return;
    }

    if (!double.TryParse(txtHeight.Text, out double height))
    {
        MessageBox.Show("Error: Height...");
        return;
    }

    //

    var gender = (BmrHelper.Gender)cboGender.SelectedIndex;

    var bmr = BmrHelper.BMR(gender, age, weight, height);

    //or this to use the metric unit instead of the default imperial unit.
    //var bmr = BmrHelper.BMR(gender, age, weight, height, BmrHelper.CalcUnit.Metric);

    lblBmrResult.Text = bmr.ToString();

    //To get the CPD:

    var activityLevel = (BmrHelper.ActivityLevel)cboActivityLevel.SelectedIndex;

    var cpd = BmrHelper.CPD(bmr, activityLevel);

    //or the overload:
    //var cpd = BmrHelper.CPD(
    //    gender,
    //    age,
    //    weight,
    //    height,
    //    activityLevel
    //    );
}

According to that, I have a very bad BMR.

Upvotes: 1

Related Questions