practice exercise

Conversion from string "" to type 'Decimal' is not v

Page 1 of 1

1 Replies - 8651 Views - Last Post: 01 May 2008 - 02:50 PM Rate Topic: -----

#1 XiJiangWoo  Icon User is offline

  • New D.I.C Head

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

practice exercise

Post icon  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

		'assign input to a variable
		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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • 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

        'assign input to a variable
        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." :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1