Visual Basic Development Bookmark and Share   
 Home > Visual Basic IDE > Help....Having trouble converting to Double from String in VB. Can anyone see the problem??
 

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

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
VB Child  Sunday, November 01, 2009 4:00 PM
a string variabledoes 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


jwavila  Sunday, November 01, 2009 4:14 PM
a string variabledoes 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


jwavila  Sunday, November 01, 2009 4:14 PM
Thank you so much! I'll try it out
VB Child  Sunday, November 01, 2009 5:03 PM

You can use google to search for other answers

Custom Search

More Threads

• Remove link formatting from copied WebBrowser text
• Usercontrol with typeconverter attribute raises a Null Object Reference when edit custom event code. Please Help!
• Line numbers in stack trace
• Problem with insert the items from ListView to DBaccess
• I have 512 ram and Semprom 2800 but Visual Web Developer run slow
• Visual Studio Debugger Not Working
• Disabling network connection using vb.net 2005
• vb 2008 How do I Deploy data files in root directory with application in its normal place
• Disappearing Data Sources
• What I see is not what I do