locked
Conversion from string "" to type 'Double' is not valid.

    Question

  • Hello, I got the error when I was trying to do validation for the part of my program where the user is asked to input the left value for the picture of the man to move to. I've got the movement of the man sorted out but I'm trying to do validation so that the user enters a value that doesn't make the picture go off screen.

    This part of the code is where if the user has entered an out of range value.  On the bottom loop i get the error "Conversion from string "" to type 'Double' is not valid."  Does anyone know how to fix this?

     If txtLeft.Text < 10 Then
                btnRelease.Enabled = False
                MsgBox("The value you entered for the left was too low", MsgBoxStyle.Exclamation, "Left Input Value Error")
                txtLeft.Text = ""

            End If

            If txtLeft.Text > 1240 Then

                btnRelease.Enabled = False
                MsgBox("The value you entered for the left was too high", MsgBoxStyle.Exclamation, "Left Input Value Error")
                MsgBox("Left:High")
                txtLeft.Text = ""
    Monday, February 23, 2009 5:29 PM

Answers

  • First off, for that code to even compile, then you must be working with Option Strict Off - a practice I don't suggest you get into the habit of.   Option Strict Off should really be reserved for cases where you are working with late bound objects (such as com automation).

    As for your error - well, it tells you exactly what the problem is.  If the txtLeft.Text is empty - has a value of "" then you are going to get an exception.  You need to check for that condition in your test:

    If txtLeft.Text.Length <> 0 AndAlso txtLeft.Text > 1240 Then 

    Of course, if I were you I would turn on Option Strict and use the Double.TryParse method or one of the built in VB methods to convert the value to a number and then test it....

    HTH




    Tom Shelton
    • Marked as answer by Yichun Feng Monday, March 02, 2009 1:34 AM
    Monday, February 23, 2009 6:10 PM
  • Hi Tom123315,

    Welcome to the Vb.Net forums. :-)

    What Tom Shelton suggests is exactly what I would do too.

    Use this kind of code within your code as TextBox values are STRINGS not numbers although they can look like a number. So in actual fact if the TextBox has 1234 in it it is a STRING as in

    Dim aString As String = "1234" 



    then it is still a STRING and always will be.


    So to see if the text will convert to a number use TryParse like this.>>

    Dim myNumber As Double = 0 
     
    Double.TryParse(txtLeft.Text,myNumber) 
     
    'If the text is "like a number" then myNumber will be the value seen in TextBox1 
    ' otherwise if the Text in the TextBox is TOM then the value then held by myNumber = 0 


    Then do your IF THEN test on the variable myNumber


    Regards,

    John

    P.S.

    I have used txtLeft.Text as you have in your code too.  ;-)

    Monday, February 23, 2009 6:55 PM

All replies

  • First off, for that code to even compile, then you must be working with Option Strict Off - a practice I don't suggest you get into the habit of.   Option Strict Off should really be reserved for cases where you are working with late bound objects (such as com automation).

    As for your error - well, it tells you exactly what the problem is.  If the txtLeft.Text is empty - has a value of "" then you are going to get an exception.  You need to check for that condition in your test:

    If txtLeft.Text.Length <> 0 AndAlso txtLeft.Text > 1240 Then 

    Of course, if I were you I would turn on Option Strict and use the Double.TryParse method or one of the built in VB methods to convert the value to a number and then test it....

    HTH




    Tom Shelton
    • Marked as answer by Yichun Feng Monday, March 02, 2009 1:34 AM
    Monday, February 23, 2009 6:10 PM
  • Hi Tom123315,

    Welcome to the Vb.Net forums. :-)

    What Tom Shelton suggests is exactly what I would do too.

    Use this kind of code within your code as TextBox values are STRINGS not numbers although they can look like a number. So in actual fact if the TextBox has 1234 in it it is a STRING as in

    Dim aString As String = "1234" 



    then it is still a STRING and always will be.


    So to see if the text will convert to a number use TryParse like this.>>

    Dim myNumber As Double = 0 
     
    Double.TryParse(txtLeft.Text,myNumber) 
     
    'If the text is "like a number" then myNumber will be the value seen in TextBox1 
    ' otherwise if the Text in the TextBox is TOM then the value then held by myNumber = 0 


    Then do your IF THEN test on the variable myNumber


    Regards,

    John

    P.S.

    I have used txtLeft.Text as you have in your code too.  ;-)

    Monday, February 23, 2009 6:55 PM
  • Thank you for your replies, they helped a lot
    Friday, March 27, 2009 4:03 PM
  • John Anthony Oliver said:
    Dim myNumber As Double = 0 
     
    Double.TryParse(txtLeft.Text,myNumber) 
     
    'If the text is "like a number" then myNumber will be the value seen in TextBox1 
    ' otherwise if the Text in the TextBox is TOM then the value then held by myNumber = 0 


    ...Maybe splitting hairs here but I think the appropriate way to use Try Parse would be to use the return value of the function, which is a boolean value.  This is important because if the value in the textbox was "0" and this was a valid input then looking to see if myNumber = 0 would be an erroneous approach.  So I'd suggest the following instead...


     
    Dim myNumber As Double = 0    
        
    If Double.TryParse(txtLeft.Text,myNumber) Then 
         'Do something with this valid numeric text value using the variable myNumber...  
    Else 
         'Handle an invalid input value...  
    End If   
     
     


    That said, of course Tom and John are correct in their views.
    • Proposed as answer by dbasnett Friday, March 27, 2009 11:50 PM
    Friday, March 27, 2009 11:30 PM
  • Good catch Dig-Boy.
    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    Friday, March 27, 2009 11:50 PM