conversion from string "" as well as a few other things

Posted 08 October 2012 - 03:36 PM

I'm having this simple problem. I have written out the code, which makes sense to me, yet still giving me a conversion error from string to double '' at the txtLabor section. I'm unsure why. The program is a simple check and txt box program that results in pricing. I looked into the conversion issue, but am not the best programmer and couldn't figure out how to apply examples to my situation. Any advice would be greatly appreciated. I'm also aware of the unchecking redundancy.. I'm just concerned with the calculation portion.

Replies To: conversion from string "" as well as a few other things

Re: conversion from string "" as well as a few other things

Posted 08 October 2012 - 04:02 PM

Tip: Don't use textboxes for numbers. Textboxes are for text. NumericUpDown is a good GUI tool for number-only input. That way you don't have to validate the user input. They can't enter a price of "Yogi bear" for example.

Re: conversion from string "" as well as a few other things

Posted 08 October 2012 - 04:14 PM

tlhIn`toq, on 08 October 2012 - 04:02 PM, said:

Tip: Don't use textboxes for numbers. Textboxes are for text. NumericUpDown is a good GUI tool for number-only input. That way you don't have to validate the user input. They can't enter a price of "Yogi bear" for example.

I only use textboxes as they hold the box in place for an input. Although, this shouldn't be the reason for my errors in this code, am I correct?

Although, this shouldn't be the reason for my errors in this code, am I correct?

Quote

a conversion error from string to double '' at the txtLabor section.

If you aren't using a textbox then you don't have to convert text to a double. A numbericUpDown doesn't have a .Text property. It uses a .Value property which is already a numeric type (Decimal).

Your error is most likely because you don't check to see if there even is text in the box. You can't convert null text to a value. '0' can be made 0. '5' can be made 5. But what is the numeric equivalent of nothing?

Re: conversion from string "" as well as a few other things

Please don't think I'm being a smart arse when I ask this because your profile doesn't say: Is English a second language for you? Are you having trouble translating my original advice?

I'm saying, don't use a textbox - use a numericUpDown instead. That way you don't have to convert text to a number... you don't have to validate the user input... The user can't enter anything that isn't a number... Your numericUpdown will always have a value even if it is 0 as the default you set in your designer.

Re: conversion from string "" as well as a few other things

You need to 'convert' the string representation of a value held in the .text property, to the appropriate target data type, in your case, this is a double, hence,

Dim Labor As Double = cdbl(txtLabor.Text)

This should also be the case for any other string to numeric assignments you make. As previously explained by using the NumericUpdown control, this would not be required, as it is already defined as a numeric value.

Re: conversion from string "" as well as a few other things

Posted 09 October 2012 - 07:08 AM

Plus using a NUD control as what I think tlhIn`toq is trying to point out will not let the user type something that will cause cdbl() to throw an exception. For example, what keeps the user from typing "Hello World" in the labor textbox?

Re: conversion from string "" as well as a few other things

Posted 09 October 2012 - 09:34 AM

dbhughes, on 08 October 2012 - 06:36 PM, said:

Private Sub BtnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalculate.Click
Dim Labor As Double = txtLabor.Text 'this is a double variable and you assign it a string value. probably should use the .TryParse method on the txtLabor.text field.
txtParts.Text = Parts.ToString("C") 'Not sure why you do this here in these next few lines, they are already strings
txtLabor.Text = Labor.ToString("C") '.ToString is usually used to convert numbers to string values
txtServNLabor.Text = ServNLabor.ToString("C") ' so that you can put them in labels or text boxes and stuff
txtTotalParts.Text = Parts.ToString("C")
txtTax.Text = Tax.ToString("C")
txtTotalFees.Text = Total.ToString("C")
End Sub