Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
<pre lang="c#"><pre lang="c#">
void BMR()
{
    int bmr = 0;
    double weight = Convert.ToInt32(tbWeight.Text);
    double height = Convert.ToInt32(tbHeight.Text);
    double age = Convert.ToInt32(tBAge.Text);
    if (rBMan.Checked)
    {
        bmr = Convert.ToInt32(66.47 + (13.75 * weight) + (5.003 * height) - (6.755 * age));
        lblBMR.Text = lblBMR.Text + bmr;
    }
    else if(rBWoman.Checked)
    {
        bmr = Convert.ToInt32(655.1 + (9.563 * weight) + (1.85 * height) - (4.676 * age));
        lblBMR.Text = lblBMR.Text + bmr;
    }
    else
    {
        MessageBox.Show("Select your Gender first", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error );
    }
}


What I have tried:

I want the program to accept only double values because strings just break it. Thank you in advance with the help!
Posted
Updated 30-Sep-20 1:14am

 
Share this answer
 
Comments
Maciej Los 30-Sep-20 7:38am    
5ed!
A better idea is to use a NumericUpDown control instead of a textbox: the value is then always numeric, and always valid / in range.
 
Share this answer
 
Comments
Richard MacCutchan 30-Sep-20 7:08am    
For doubles?
OriginalGriff 30-Sep-20 7:32am    
All of his code shows Convert.ToInt32
Richard MacCutchan 30-Sep-20 7:38am    
Ha, ha, I missed that. So I wonder why he wants double values?
Double.TryParse Method (System) | Microsoft Docs[^]
C#
void BMR()
{
    if (!double.TryParse(tbWeight.Text, out var weight))
    {
        MessageBox.Show("Enter a valid weight", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    if (!double.TryParse(tbHeight.Text, out var height))
    {
        MessageBox.Show("Enter a valid height", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }
    if (!double.TryParse(tBAge.Text, out var age))
    {
        MessageBox.Show("Enter a valid age", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }

    int bmr = 0;
    if (rBMan.Checked)
    {
        bmr = Convert.ToInt32(66.47 + (13.75 * weight) + (5.003 * height) - (6.755 * age));
        lblBMR.Text = lblBMR.Text + bmr;
    }
    else if(rBWoman.Checked)
    {
        bmr = Convert.ToInt32(655.1 + (9.563 * weight) + (1.85 * height) - (4.676 * age));
        lblBMR.Text = lblBMR.Text + bmr;
    }
    else
    {
        MessageBox.Show("Select your Gender first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
 
Share this answer
 
Comments
Maciej Los 30-Sep-20 7:38am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900