Problems with calculations and conversions in Visual Basic Form
In the code below I get the "Conversion from string "" to type 'Double' is not valid." error. Can anyone let me know why this is ? Thanks.
Public Class Form1
Dim SalesDecimal As Decimal
Const BasePay As Decimal = 250D
Const CommissionRate As Decimal = 0.15D
Dim Quota As Integer = 1000
Dim TotalSalesAmt, TotalComAmt, GrandTotal As Decimal
Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click
SalesDecimal = Decimal.Parse(TextBox3.Text)
TextBox2.Text = Decimal.Parse(CommissionCalc(SalesDecimal))
***TextBox4.Text = Decimal.Parse(CommissionRate * SalesDecimal)***
If TextBox3.Text < 1000 Then
TextBox4.Text = ""
End If
TotalSalesAmt += TextBox3.Text
TotalComAmt += TextBox4.Text
GrandTotal += TextBox2.Text
End Sub
Private Function CommissionCalc(ByVal Sales As Integer) As Decimal
Dim Total = (SalesDecimal * CommissionRate) + BasePay
If Sales >= Quota Then
Return Total
Else
Return BasePay
End If
End Function
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End Sub
Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
With ColorDialog1
ColorDialog1.ShowDialog()
TextBox2.ForeColor = .Color
End With
End Sub
Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
W开发者_高级运维ith FontDialog1
.ShowDialog()
TextBox2.Font = .font
End With
End Sub
Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click
Dim MessageString As String
MessageString = "Amount of sales: " & TotalSalesAmt.ToString("C") _
& Environment.NewLine & Environment.NewLine _
& "Total Commissions: " & TotalComAmt.ToString("C") _
& Environment.NewLine & Environment.NewLine _
& "Total Pay: " & GrandTotal.ToString("C")
MessageBox.Show(MessageString, "Sales Summary", MessageBoxButtons.OK)
End Sub
End Class
You need to explicitly cast your variables when pushing them into the text box.
If TextBox3.Text < 1000 Then
TextBox4.Text = ""
End If
TotalSalesAmt += TextBox3.Text
TotalComAmt += TextBox4.Text
GrandTotal += TextBox2.Text
For instance your TextBox3.Text above is a String, not a Decimal. Your trying to move through types without casting them to the appropriate type.
TextBox2.Text = Decimal.Parse(CommissionCalc(SalesDecimal))
You are using Decimal.Parse incorrectly. Decimal.Parse is used to parse a String to a Decimal type, you seem to be using it to do both a string to decimal and decimal to string. If you want to get the string representation of the decimal value you should use ToString. In the example above form your code, CommisionCalc returns a Decimal number, you need to call CommissionCalc(SalesDecimal).ToString() not Decimal.Parse()
精彩评论