When I try to run the program to calculate payment and total intrest I get \"Con
ID: 3650197 • Letter: W
Question
When I try to run the program to calculate payment and total intrest I get "Conversion from string "" to type 'Double' is not valid."
What am I doing wrong?
Dim P As Double
Dim R As Double
Dim N As Double
Dim Payment As Double
Dim totalInterest As Double
Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
P = CDbl(txtAmount.Text)
N = CDbl(txtDuration.Text)
R = CDbl(txtInterestRate.Text)
Payment = (P * R) / (1 - (1 + R) ^ (-N))
totalInterest = (N * Payment) - P
Payment = CDbl(txtPayment.Text)
totalInterest = CDbl(txtInterest.Text)
If P < 0 Then
MessageBox.Show("Please enter in loan amount")
End If
If R <= 0 Then
MessageBox.Show("Please enter in loan amount")
End If
If N <= 0 Then
MessageBox.Show("Please enter in loan amount")
End If
End Sub
End Class
Explanation / Answer
Conversion from string "" to type 'Double' is not valid." It's complaining about when string is empty,so add if statements to take care of that. ____________________________________________ Dim P As Double Dim R As Double Dim N As Double Dim Payment As Double Dim totalInterest As Double Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click If txtAmount.Text="" Then txtAmount.Text=0 End If If txtDuration.Text="" Then txtDuration.Text=0 End If If txtInterestRate.Text="" Then txtInterestRate.Text=0 End If P = CDbl(txtAmount.Text) N = CDbl(txtDuration.Text) R = CDbl(txtInterestRate.Text) Payment = (P * R) / (1 - (1 + R) ^ (-N)) totalInterest = (N * Payment) - P Payment = CDbl(txtPayment.Text) totalInterest = CDbl(txtInterest.Text) If P < 0 Then MessageBox.Show("Please enter in loan amount") End If If RRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.