locked
conversion from string to type double is not valid RRS feed

  • Question

  • please help me in my assignment. i always got error every time i  run this program . here's the code.

     

     'declaring variables
      
        Const DEPOSIT As Double = 30
        Dim dbltotal As Double
        Dim strequipment As String
        Dim intitem As Integer
        Dim dblrental As Double
        Dim strdurationinput As String

        Dim strfmt As String = "{0,15} {1,10} {2,18}"
        Dim strduration As String



      
        Private Sub btnrentalrates_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnrentalrates.Click
            'display the rental rates in the upper list box
            lstdisplay.Items.Add("Price of Equipment   Half-Day     Full-day")
            lstdisplay.Items.Add("")
            lstdisplay.Items.Add("1. Rug Cleaner        $16.00       $24.00")
            lstdisplay.Items.Add("2. Lawn Mower         $12.00       $18.00")
            lstdisplay.Items.Add("3. Paint Sprayer      $20.00       $30.00")

        End Sub

        Private Sub btndisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndisplay.Click

            ' input validation
            If txtselectitem.Text < 1 Or txtselectitem.Text > 3 Then- "this is the code that gives me error when i click the calculate button without putting any input"
                MsgBox("Input numbers 1 up to 3 only", , "Input Error")
                txtselectitem.Focus()
                Exit Sub
            Else
                intitem = txtselectitem.Text

            End If

            If txtduration.Text.ToUpper <> "H" And txtduration.Text.ToUpper <> "F" Then
                MsgBox("Input H or F only", , "Input Error")
                txtduration.Focus()
                Exit Sub
            Else
                strduration = txtduration.Text

            End If

    Sunday, October 16, 2011 3:01 AM

Answers

  • Try this code:

     

    Dim selItem as Double = 0
    
    Dim result as Boolean = Double.TryParse(txtselecteditem.Text, selItem)
    
    If selItem < 1 Or selItem > 3 Then- "this is the code that gives me error when i click the calculate button without putting any input"
                MsgBox("Input numbers 1 up to 3 only", , "Input Error")
                txtselectitem.Focus()
                Exit Sub
            Else
                intitem = selItem
    


     


    Tom Overton

    • Edited by Tom_Overton Sunday, October 16, 2011 3:22 AM
    • Proposed as answer by Mike Feng Monday, October 17, 2011 11:23 AM
    • Marked as answer by Mike Feng Wednesday, November 2, 2011 10:25 AM
    Sunday, October 16, 2011 3:20 AM
  • Mariles,

    You take a very old program writing way. 

    You use Visual Basic Net like it is Cobol or Fortran. 

    You start with a working storage section to declare all your variables, declare them only global (and then with private) when you really need them on more places.

    However, your error is easy to do

            If IsNumeric(txtSelectItem.Text) Then
                If txtselectitem.Text < 1 OrElse txtselectitem.Text > 3 Then
                End If
            End If
    


    Be aware this IsNumeric is not in C# therefore is in framework 2.0 created the TryParse which can also be used in VB, typical C# programmers seldom use the IsNumeric. In fact those functions do the same because also the IsNumeric is using behind the scene a try catch.

     


    Success
    Cor
    • Proposed as answer by Mike Feng Monday, October 17, 2011 11:23 AM
    • Marked as answer by Mike Feng Wednesday, November 2, 2011 10:25 AM
    Sunday, October 16, 2011 8:25 AM

All replies

  • Try this code:

     

    Dim selItem as Double = 0
    
    Dim result as Boolean = Double.TryParse(txtselecteditem.Text, selItem)
    
    If selItem < 1 Or selItem > 3 Then- "this is the code that gives me error when i click the calculate button without putting any input"
                MsgBox("Input numbers 1 up to 3 only", , "Input Error")
                txtselectitem.Focus()
                Exit Sub
            Else
                intitem = selItem
    


     


    Tom Overton

    • Edited by Tom_Overton Sunday, October 16, 2011 3:22 AM
    • Proposed as answer by Mike Feng Monday, October 17, 2011 11:23 AM
    • Marked as answer by Mike Feng Wednesday, November 2, 2011 10:25 AM
    Sunday, October 16, 2011 3:20 AM
  • Mariles,

    You take a very old program writing way. 

    You use Visual Basic Net like it is Cobol or Fortran. 

    You start with a working storage section to declare all your variables, declare them only global (and then with private) when you really need them on more places.

    However, your error is easy to do

            If IsNumeric(txtSelectItem.Text) Then
                If txtselectitem.Text < 1 OrElse txtselectitem.Text > 3 Then
                End If
            End If
    


    Be aware this IsNumeric is not in C# therefore is in framework 2.0 created the TryParse which can also be used in VB, typical C# programmers seldom use the IsNumeric. In fact those functions do the same because also the IsNumeric is using behind the scene a try catch.

     


    Success
    Cor
    • Proposed as answer by Mike Feng Monday, October 17, 2011 11:23 AM
    • Marked as answer by Mike Feng Wednesday, November 2, 2011 10:25 AM
    Sunday, October 16, 2011 8:25 AM