vb.net "从字符串 "" 到类型 'Double' 的转换无效。" 在VB中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12632245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Conversion from string "" to type 'Double' is not valid." In VB
提问by compucrazy
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
回答by Reed Copsey
One of your TextBox
items has not been filled in.
您的其中一项TextBox
尚未填写。
As such, when you use CDbl
, such as P = CDbl(txtAmount.Text)
, if the TextBox
is empty, it will cause this error.
因此,当您使用CDbl
,例如 时P = CDbl(txtAmount.Text)
,如果TextBox
为空,则会导致此错误。
A better option would be to use Double.TryParseinstead of CDbl
, as it will allow you to raise a proper message:
更好的选择是使用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
回答by Rajesh Pathakoti
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..
如果文本框为空,则使用函数的 Val() 代替 CDbl(),它将字符串转换为 0 默认值。那么运行时错误可能不会出现..
回答by Ryan Harger
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)
到目前为止,在某种程度上,每个人都是对的。您尝试的 CDbl 函数不能很好地处理空白(它会爆炸)。在某些情况下(特别是我的)TryParse 不是一个选项(请参阅下面的“为什么”以及真正的解决方案)
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).
但是,如果您期望数据中有数字,并且不可能收到错误(长话短说,这就是我的上下文),那么您必须使用 CDbl,因为没有另一种转换方法可用于一个单一的表达(再次,我的上下文)。
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.
就我而言,由于我无法声明变量,因此我找到了一个优雅的解决方案。我是用连接器完成的,在我的情况下(我使用的是 vb.net )它是CDbl("0" + txtAmount.Text)
P 是您测试的原始数据,然后是 R 和 N 等。
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
这和以前Val()
一样,默认前导零,您没有更改变量中的任何数据,而只是解决了空白。有更多整体解决方案,但我无法利用这些解决方案,因为我只能访问“单行”可以这么说
回答by Mark Hurd
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)