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 TextBox items has not been filled in.
As such, when you use CDbl, such as P = CDbl(txtAmount.Text), if the TextBox is 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:
Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
If Not Double.TryParse(txtAmount.Text, P) Then
MessageBox.Show("Please correct the loan amount")
Exit Sub
End If
' Do the same for all other CDbl checks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With