Solved

# Text Boxes and Int

Posted on 2004-11-13
205 Views
In the following code, I get an error stating "      Error      1 Cannot implicitly convert type 'int' to 'string".  I'm new at this and have no clue what's going on LOL.  Any help is appreciated.  I am trying to get the int "BMI" to display in the text box txtBMI.  Also, am I correct in "Height = int.Parse(txtHeight.text)" etc?

private void cmdCalculate_Click(object sender, EventArgs e)
{
int X;
int Y;
int Height;
int Weight;
int Z = 703;
int BMI;

Height = int.Parse(txtHeight.Text);
Weight = int.Parse(txtWeight.Text);
X = Weight / Z;
Y = X / Height;
BMI = Y / Height;

txtBMI.Text = BMI;

}
0

LVL 9

Expert Comment

You're getting the problem in the

txtBMI.Text = BMI;
line

the problem is that the Text property is of type string and BMI is an integer

try using

txtBMI.Text = BMI.ToString();

Tincho
0

LVL 1

Author Comment

tinchos,

That works, but it's giving me a value of "0"....  is my math wrong somewhere?
0

LVL 9

Expert Comment

If its showing a 0 then it's because that's the value of the variable

tell mewhat is it you're trying to get... so I can check it out

Tincho
0

LVL 9

Expert Comment

Keep in mind that the result of the division is an integer, so it cannot have decimal values....

so, if the result is 0.7, the result is going to be 0

Tincho
0

LVL 1

Author Comment

It's a body mass index (BMI) calculator.  I takes your weight (txtWeight), multiplies it by 703 (int Z), divides by your height in inches twice (txtHeight).  I hope that makes sense...  it's hard to put in writing :)

The result should be somewhere between 18.5 and 30, usually.
0

LVL 9

Expert Comment

Just to a little math

if

X = Weight / Z;
Y = X / Height;
BMI = Y / Height;

txtBMI.Text = BMI;

then

BMI = Y / Height; and Y = X / Height;

so

BMI = X / ( Height * Height ) and  X = Weight / Z;

so

BMI = Weight / (Height * Height * 703)

Keep in mind that in order to get BMI > 0

Weight  > Height * Height * 703

Tincho
0

LVL 1

Author Comment

But, technically...  that doesn't work....  say weight is 150....  and height is 64 inches....

150 * 703 = 105450
105450 / 64 = 1647.65625
1647.65625 / 64 = 25.74462890625

Going with Weight / (height * height * 703).....

150 / (64 * 64 * 703)....
150 / (4096 * 703)...
150 / 2879488 = 19196.586666666666666666666666667

Two completely different numbers....  now, I'm not excellent at math, so I could have the order of operations completely twisted...  if so, please correct me.
0

LVL 9

Expert Comment

If the first piece is what you're trying to get, then you must check that in your code you're making

X = Weight / Z;                      // In your last post you're making     X = Weight * Z instead of it
Y = X / Height;
BMI = Y / Height;

Tincho
0

LVL 9

Accepted Solution

If you want to accomplish

150 * 703 = 105450
105450 / 64 = 1647.65625
1647.65625 / 64 = 25.74462890625

then you should make

private void cmdCalculate_Click(object sender, EventArgs e)
{
int X;
int Y;
int Height;
int Weight;
int Z = 703;
int BMI;

Height = int.Parse(txtHeight.Text);
Weight = int.Parse(txtWeight.Text);
X = Weight * Z;
Y = X / Height;
BMI = Y / Height;

txtBMI.Text = BMI;

}

Hope this helps

Tincho
0

LVL 1

Author Comment

Tincho,

Works great!  Thank you so much for the help, I really appreciate it!!!

Tony
0

## Featured Post

### Suggested Solutions

In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…