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
One of your
TextBoxitems has not been filled in.As such, when you use
CDbl, such asP = CDbl(txtAmount.Text), if theTextBoxis empty, it will cause this error.A better option would be to use Double.TryParse instead of
CDbl, as it will allow you to raise a proper message: