# practice exercise

Page 1 of 1

## 1 Replies - 20743 Views - Last Post: 01 May 2008 - 02:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=50754&amp;s=2842eed5fa36bca8f9eb538e585932a1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 XiJiangWoo

Reputation: 0
• Posts: 24
• Joined: 06-April 08

# practice exercise

Posted 01 May 2008 - 01:23 PM

Hello everyone,

I m doing a practice exercise and I"m having trouble getting the results. The program asks the user to input gender, activity level, and weight to determine the daily allowance of calories to maintain the current weight.

I keep getting an error "Conversion from string "" to type 'Decimal' is not valid." Here is my code

```Option Explicit On
Option Strict Off

Public Class MainForm

Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub

Private Sub displayCaloriesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayCaloriesButton.Click
' declare constants and variables

Dim gender As String
Dim activity As String
Dim weight As Decimal
Dim isConverted As Boolean
Dim totalCal As Decimal

gender = genderTextBox.Text
activity = activityTextBox.Text
weight = weightTextBox.Text

'convert number input to integers
isConverted = Decimal.TryParse(weightTextBox.Text, weight)
isConverted = Decimal.TryParse(caloriesLabel.Text, weight)

' calculate and display the daily calories required to maintain weight
totalCal = caloriesLabel.Text

If gender = "F" AndAlso activity = "A" Then
totalCal = weight * 12
caloriesLabel.Text = totalCal.ToString("c2")
ElseIf gender = "F" AndAlso activity = "I" Then
totalCal = weight * 10
caloriesLabel.Text = totalCal.ToString("c2")
ElseIf gender = "M" AndAlso activity = "A" Then
totalCal = weight * 15
caloriesLabel.Text = totalCal.ToString("c2")
ElseIf gender = "M" AndAlso activity = "I" Then
totalCal = weight * 15
caloriesLabel.Text = totalCal.ToString("c2")
End If

End Sub
End Class

```

As always, any help is greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: practice exercise

### #2 Martyr2

• Programming Theoretician

Reputation: 5207
• Posts: 13,955
• Joined: 18-April 07

## Re: practice exercise

Posted 01 May 2008 - 02:50 PM

First of all you are going to want to use "double" and not "decimal". Decimal is for money and something tells me that you are not trying to find money when calculating calories.

It is also going to cry foul when you try to stick something like the caloriesLabel into the totalCalc variable directly because it is a string and going into a decimal without conversion is probably what is causing your error.

I am also not sure why you attempt to parse the weighttextbox.text into weight, then try to do the label into weight, the same variable. This would cause you to overwrite the weight variable.

And lastly, you should be putting the totalCal into your label AFTER you have calculated it, not before. So try something like this...

``` Private Sub displayCaloriesButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles displayCaloriesButton.Click
' declare constants and variables

Dim gender As String
Dim activity As String
Dim weight As Double
Dim isConverted As Boolean
Dim totalCal As Double

gender = genderTextBox.Text
activity = activityTextBox.Text
weight = weightTextBox.Text

'convert number input to integers
isConverted = Double.TryParse(weightTextBox.Text, weight)

If gender = "F" AndAlso activity = "A" Then
totalCal = weight * 12
ElseIf gender = "F" AndAlso activity = "I" Then
totalCal = weight * 10
ElseIf gender = "M" AndAlso activity = "A" Then
totalCal = weight * 15
ElseIf gender = "M" AndAlso activity = "I" Then
totalCal = weight * 15
End If

' calculate and display the daily calories required to maintain weight
caloriesLabel.Text = totalCal
End Sub

```

Enjoy!

"At DIC we be weight control code ninjas...we hunt them down and kill them to control how much weight is actually out there."