gpt4 book ai didi

vb.net - "Conversion from string ""to type ' 双' is not valid."在VB

转载 作者:行者123 更新时间:2023-12-04 23:52:46 25 4
gpt4 key购买 nike

当我尝试运行程序来计算付款和总利息时,我得到“从字符串转换为类型 'Double' 无效。”

我做错了什么?

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

最佳答案

您的 TextBox 项之一尚未填写。

因此,当你使用CDbl时,如P = CDbl(txtAmount.Text),如果TextBox为空,则会导致这个错误。

更好的选择是使用 Double.TryParse而不是 CDbl,因为它可以让您发出正确的消息:

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

关于vb.net - "Conversion from string ""to type ' 双' is not valid."在VB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12632245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com