compucrazy
compucrazy

Reputation: 65

"Conversion from string "" to type 'Double' is not valid." In VB

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

Upvotes: 3

Views: 103739

Answers (4)

Ryan Harger
Ryan Harger

Reputation: 57

I know this original question is old, but it's trending on Google.

Everyone has been right, to a degree, so far.. The CDbl function you were attempting doesn't handle blanks very well (it blows up). In some cases (mine specifically) TryParse isn't an option (see below for 'why' along with true solution)

IF, however, you are expecting numbers in the data, and receiving an error isn't possible (long story short, that is my context) then you HAVE to use CDbl, since there isn't another conversion method that can be used in a single expression (again, my context).

In my case, since i wasn't able to declare variables, i found an elegant solution this problem. I did it with a concatenator, in my case (I'm using vb.net ) it was CDbl("0" + txtAmount.Text) where P is the original data your testing for, and then R and N, etc.

This does what Val() used to do, by defaulting a leading zero, you have not changed any of the data within the variable, but have only solved blanks. There are more wholistic solutions, but I couldn't utilize those since I only had access to "one-liners" so-to-speak

Upvotes: 2

Rajesh Pathakoti
Rajesh Pathakoti

Reputation: 659

Instead of CDbl(),use Val() of function it converts the string into 0 default,if the text box is empty. Then runtime error may not come..

Upvotes: 1

Mark Hurd
Mark Hurd

Reputation: 10931

I'd say it is simply because you want:

txtPayment.Text = CStr(Payment)
txtInterest.Text = CStr(totalInterest)

instead of

Payment = CDbl(txtPayment.Text)
totalInterest = CDbl(txtInterest.Text)

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564363

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

Upvotes: 4

Related Questions