VB Help: Fix "Conversion from string to type 'Double' is not valid" Error

  • Thread starter youngsc
  • Start date
  • Tags
    Visual
In summary, the individual is having trouble with converting a string to a double in VB 2008 and then dividing two values. They receive an error message when attempting to do so and have tried changing the code to use C# instead. The expert suggests converting the text to a double before attempting to divide and reminds the individual to do math on numbers, not strings or UI elements.
  • #1
youngsc
3
0
Ok I'm just starting to teach myself and I thought I could have at least gotten this part but I'm getting a Conversion from string "" to type 'Double' is not valid. error message.

Code:
Private Sub StockPricetxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StockPricetxt.TextChanged
        Dim StockPricetxt As Double
        StockPricetxt = ""
    End Sub

What's happening when I test it is the second you put anything in the text box it freezes and return the error message above. The larger code will be to take this value and another and divide them. I'm assuming it has to do with the use of double but I'm lost. I would appreciate any help you all could give me. Using VB 2008 btw.


Also, great forum I've been surfing it for a little while now and it's a lot of good info to be had.
 
Technology news on Phys.org
  • #2
Ok that problem is solved now I got a new one.

Code:
    Private Sub StockPricetxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StockPricetxt.TextChanged
        Dim StockPricetxt As Double
        StockPricetxt = CDbl(StockPricetxt)

    End Sub

    Private Sub EPStxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EPStxt.TextChanged
        Dim EPStxt As Double
        EPStxt = CDbl(EPStxt)
    End Sub

    [B]Private Sub PEtxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PEtxt.TextChanged
        Dim PEtxt As Double 
        PEtxt = StockPricetxt / EPStxt[/B]

    End Sub

I'm trying to divide these values and the last sub bolded I'm having trouble with. It's telling me "Operator '/' is not defined for types 'System.Windows.Forms.TextBox'". Any ideas? I am searching this stuff on the web and can't come up with exact results to give me what I need.

TIA
 
  • #3
Are you attempting to do arithmetic on text? (i.e. a string?).

Convert the text to a double before attempting the divide.
 
  • #4
zeitghost said:
Are you attempting to do arithmetic on text? (i.e. a string?).

Convert the text to a double before attempting the divide.

Thanks for the reply. I got it figured out, I used C# instead. I'm trying to teach myself quite a bit of this and I'm doing as much of it "example" based as possible. I couldn't find any good examples in VB but found a lot in C#. Thanks again.
 
  • #5
C# is better than Visual Basic, to be sure. Just be sure to do math on numbers, not strings or UI elements.
 

What does the error "Conversion from string to type 'Double' is not valid" mean?

This error means that there is an attempt to convert a string value to a double value, but the string cannot be converted to a valid double.

Why am I getting this error?

This error is most commonly caused by trying to convert a string that contains non-numeric characters or is empty to a double value. It can also occur when the string contains a decimal separator that is not recognized by the system.

How can I fix this error?

To fix this error, you will need to ensure that the string you are trying to convert to a double is a valid numeric value. You can also use error handling techniques, such as Try-Catch blocks, to handle any invalid conversions.

Can I convert other data types to a double without getting this error?

Yes, you can convert other numeric data types, such as Integer or Decimal, to a double without getting this error as long as the value is within the range of a double data type.

Is there a way to prevent this error from occurring?

To prevent this error, you can use data validation techniques to ensure that the user enters a valid numeric value before attempting to convert it to a double. You can also use the IsNumeric function to check if a string is a valid numeric value before attempting to convert it.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top