Microsoft 开发人员网络 >
论坛主页
>
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
答案
- 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
- 已编辑jwavila 2009年11月1日 17:06
- 已标记为答案Jeff ShanMSFT, Moderator2009年11月5日 2:30
全部回复
- 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
- 已编辑jwavila 2009年11月1日 17:06
- 已标记为答案Jeff ShanMSFT, Moderator2009年11月5日 2:30
- Thank you so much! I'll try it out

