为什么Windows 10使此代码产生InvalidCastException [英] Why is Windows 10 making this code give an InvalidCastException
问题描述
以下代码片段在Windows 7专业版上运行时效果非常好但在Windows 10上运行时会抛出无效的强制转换异常。
任何想法微软改变了什么以及我需要做些什么才能获得它在Win10上工作吗?
这是我的一个朋友在Win10 pc和它上面运行它我没有在我的电脑上:-)
非常感谢,
Tim
**************异常文本**************
The following code snippet works absolutely great when run on Windows 7 professional BUT throws an Invalid Cast Exception when run on Windows 10.
Any idea what Microsoft have changed and what I need to do to get it working on Win10?
It's a friend of mine running it on a Win10 pc & I don't have it on my pc :-)
Many thanks,
Tim
************** Exception Text **************
System.InvalidCastException: Conversion from string "£0.00" to type 'Decimal' is not valid.
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDecimal(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDecimal(String Value)
at GtracsX4.gtac1b.tBox_Validating(Object sender, CancelEventArgs e)
代码
The Code
Private Sub tBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
amtInp.Validating, DescInp.Validating, RefInp.Validating
Dim mtb As String = sender.tag : Dim mtbx As TextBox = sender : Dim ta As Decimal
If mtb = "dec" Then
If (String.IsNullOrEmpty(mtbx.Text)) Then mtbx.Text = "0"
If IsNumeric(mtbx.Text) = False Then
msgM = "Only NUMBERS allowed. Please re-enter"
msg = New gtzMess(1, "", msgM, "OK", "", "", "S") ' flags = ISQW
msg.ShowDialog()
mtbx.Text = 0
e.Cancel = True
Exit Sub
End If
ta = CDec(mtbx.Text)
mtbx.Text = "£" & ta.ToString("N2")
If mtbx.Name = "amtInp" Then tAmt.Text = CDec(mtbx.Text)
End If
End Sub
推荐答案
你为什么这样做?
看看你的代码:
Why are you doing that?
Look at your code:
ta = CDec(mtbx.Text)
mtbx.Text = "£" & ta.ToString("N2")
If mtbx.Name = "amtInp" Then tAmt.Text = CDec(mtbx.Text)
ta = CDec(mtbx.Text)
取一个文本框值,然后转换它到十进制。这个想法很糟糕:你应该使用Decimal.TryParse并向用户报告问题。
Take a Textbox value, and convert it to a Decimal. This is a poor idea: you should be using Decimal.TryParse and reporting problems to the user.
mtbx.Text = "£" & ta.ToString("N2")
将它转换为十进制值后,立即将其转换回tring,并在前面加上货币符号。
Having converted it to a decimal value, you immediately convert it back to a tring, and tack a currency symbol on the front.
If mtbx.Name = "amtInp" Then tAmt.Text = CDec(mtbx.Text)
然后你将它转换回一个十进制(如果它不会导致它失败,它将丢弃该货币),以便将其转换回一个字符串,将其放入文本框。
那是......嗯......很奇怪。
使用TryParse转换它(带报告),看看你想要存储在哪里 - 你最多需要两种类型的转换:一个到一个dec,一个到一个字符串。这只是因为你指定了字符串值的格式!
你的问题会在同一时间消失。
You then convert that back to a decimal (which will throw away the currency, if it doesn't cause it to fail, which it does) in order to convert it back into a string, to put it in a textbox.
That's...um...very odd.
Use TryParse to convert it (with reporting), and look at what you are trying to store where - you should need at most two type conversions: one to a dec, and one to a string. And that's only because your specify a format for the string value!
And your problem will go away at the same time.
它与Windows版本无关,它与您描述的两种不同情况下设置的不同默认文化有关。您的代码应该是文化中立的,或者您必须在应用程序本身强制某些固定的文化,这是一个每个线程的选项:
https://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture (v = vs.110).aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture(v = vs.110)的.aspx [ ^ ]。
-SA
It is not related to Windows version, it's related to the different default culture set up in two different cases you described. Your code should either be culture-neutral, or you have to force certain fixed culture in the application itself, which is a per-thread option:
https://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture(v=vs.110).aspx[^].
—SA
这篇关于为什么Windows 10使此代码产生InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!