字符串加倍无效吗? [英] String to Double not Valid?

查看:99
本文介绍了字符串加倍无效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


作为使用vb 2008和Office 2007对Excel Automation进行持续测试的一部分,我决定在本地计算机中创建虚拟机.

我仍在尝试在另一个线程中发布的InterOp程序集中找到解决方案.

在查询表单中,我将文本框值声明为字符串,然后将其转换为double并在我的Select语句中使用,

Hi,
As part of my on-going testing of Excel Automation using vb 2008 and Office 2007, I decided to create a virtual Machine in my Local computer.

I''m still trying to find solution bout the InterOp assembly which I posted in another thread.

In my Query Form, I declared the Textbox value as string, then converted to double and is use in my Select Statement,

Dim Rate1 as String = Me.Textbox6.Text
Dim nRate as Double = CDbl(Rate1)


在我的本地环境中对此进行了测试,并且可以正常工作.创建一个SetUp并将该Setup复制到我的虚拟机并进行安装,程序运行正常,并且我可以生成views(DGV)和outputs(文本文件).一些报告使用TextBox6及其值,但这是我收到此错误将字符串转换为双精度无效吗?"的地方,

在必备组件"中,我对此进行了检查;


Tested this in my local and is working Okay. Created a SetUp and copied the Setup to my Virtual Machine and Install It, the program is working okay and i can produce views(DGV) and outputs(textfile). Some reports uses the TextBox6 and its value, but this is where I recieve this Error "Convert String to double is Invalid?",

in Prerequisite Components, i checked this;

Windows Installer 3.1<br />
.Net Framework 3.5 SP1<br />
Microsoft Office 2007 Primary InterOp Assemblies



我还在虚拟机中安装了Windows XP和Office2007.



I have also installed windows xp and office 2007 in my virtual machine.

推荐答案

好吧,并非所有String都兼容Double.例如,如何将"Hi"或空的String转换为数字值?
我当然看不到您的数据集,但似乎那里有一些非数字值.
更加安全的是调用 Double.TryParse [ ^ ].
用法如下:
Well, not all Strings are compatible Doubles. How would, for example, "Hi" or an empty String be converted to a numeric value?
Of course I can''t see your dataset, but it seems there are some non-numeric values in there.
What would be a lot safer is to call Double.TryParse[^].
Usage as follows:
Dim d As Double ' If only this were C#, we would've had Double d :)
If Double.TryParse(Me.TextBox6.Text, d) Then
   ' The string was successfully converted.
   ' Your variable d now has the converted value.
Else
   ' The string was not a numeric value.
   ' Nothing happened to d.
End If


使用
Dim nRate as Double=Double.Parse(Rate1 );


在转换文本框文本之前,必须对其进行一些验证.

如果TextBox6为空白,则CDbl将引发您看到的错误.

如果最好改用Double.Parse()或.TryParse(),但在尝试解析之前,仍必须确保文本框具有值.
You have to do some validation of the textbox text before you convert it.

If TextBox6 is blank, CDbl will throw the error you''re seeing.

If would be better to use Double.Parse() or .TryParse() instead, but you still have to make sure the textbox has a value before you try and parse it.


这篇关于字符串加倍无效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆