Visual Basic > Visual Basic Forums > Visual Basic IDE > Help....Having trouble converting to Double from String in VB. Can anyone see the problem??
Ask a questionAsk a question
 

AnswerHelp....Having trouble converting to Double from String in VB. Can anyone see the problem??

  • Sunday, November 01, 2009 4:00 PMVB Child Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Public Class frmWaiter
    
      Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    
        'Bill Amount Input Box
        Dim txtBillAmount As String
        Dim txtBillTitle As String
        Dim txtBillPrompt As String
        Dim dblBillAmount As Double
    
        txtBillTitle = "Bill Amount"
        txtBillPrompt = "Enter the bill amount"
        txtBillAmount = InputBox(txtBillPrompt, txtBillTitle)
        dblBillAmount = CDbl(txtBillAmount.Text)
    
        'Tip Amount Input Box
        Dim txtTipAmount As String
        Dim txtTipTitle As String
        Dim txtTipPrompt As String
        Dim dblTipAmount As Double
        txtTipTitle = "Tip Amount"
        txtTipPrompt = "Enter the tip amount in percent"
        txtTipamount = InputBox(txtTipPrompt, txtTipTitle)
        dblTipAmount = CDbl(txtTipAmount)
    
        'Result Calculation
        Dim dblResult As Double
        Dim txtResult As String
        dblResult = dblBillAmount * dblTipAmount
        txtResult = CStr(dblResult)
    
        txtResult = CStr(txtResult)
    
    
    
    
      End Sub
    
    End Class
    

Answers

  • Sunday, November 01, 2009 4:14 PMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    a string variable does not have a .Text property, so this line should be:


    dblBillAmount = CDbl(txtBillAmount)
    

    however, I would do a couple things differently to shorten your code.

    First put your variable declaration in the same line as your InputBox. And I would just put the Title and Prompt in the overloads for the InputBox instead of creating a variable.

    And use Double.TryParse instead of CDbl, unless you never make a typing mistake. try typing 25w in your first inputbox and see what I mean.

    then a little trick: use FormatCurrency to display the results.

    try this in a test app with 1 button and 1 label


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim txtBillAmount As String = InputBox("Enter the Bill Amount", "Bill Amount")
            Dim txtTipAmount As String = InputBox("Enter the tip amount in percent", "Tip Amount")
    
            Dim dblBillAmount As Double
            Dim dblTipAmount As Double
    
            If Not Double.TryParse(txtBillAmount, dblBillAmount) OrElse Not Double.TryParse(txtTipAmount, dblTipAmount) Then
                MessageBox.Show("Invalid entry")
                Exit Sub
            End If
    
            dblTipAmount /= 100
    
            'Result Calculation
            Label1.Text = "Amount of Tip: " & FormatCurrency(dblBillAmount * dblTipAmount)
    
        End Sub
    


All Replies

  • Sunday, November 01, 2009 4:14 PMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    a string variable does not have a .Text property, so this line should be:


    dblBillAmount = CDbl(txtBillAmount)
    

    however, I would do a couple things differently to shorten your code.

    First put your variable declaration in the same line as your InputBox. And I would just put the Title and Prompt in the overloads for the InputBox instead of creating a variable.

    And use Double.TryParse instead of CDbl, unless you never make a typing mistake. try typing 25w in your first inputbox and see what I mean.

    then a little trick: use FormatCurrency to display the results.

    try this in a test app with 1 button and 1 label


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim txtBillAmount As String = InputBox("Enter the Bill Amount", "Bill Amount")
            Dim txtTipAmount As String = InputBox("Enter the tip amount in percent", "Tip Amount")
    
            Dim dblBillAmount As Double
            Dim dblTipAmount As Double
    
            If Not Double.TryParse(txtBillAmount, dblBillAmount) OrElse Not Double.TryParse(txtTipAmount, dblTipAmount) Then
                MessageBox.Show("Invalid entry")
                Exit Sub
            End If
    
            dblTipAmount /= 100
    
            'Result Calculation
            Label1.Text = "Amount of Tip: " & FormatCurrency(dblBillAmount * dblTipAmount)
    
        End Sub
    


  • Sunday, November 01, 2009 5:03 PMVB Child Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you so much!  I'll try it out