错误输入字符串的格式不正确。 [英] Error input string was not in a correct format.

查看:80
本文介绍了错误输入字符串的格式不正确。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Input string was not in a correct format.



我尝试将vb转换为c#以获取双倍值,但我收到此错误


I try to convert vb to c# for double value but i got this error "

Input string was not in a correct format."





这是VB



Here is VB

If CDbl(TxtBx_ClaimValue.Text) = CDbl(0) Then
    Lbl_Message.Text = "Error. Claim Value ?"
    TxtBx_ClaimValue.Focus()
    Exit Sub
End If





这是C#



Here is C#

if (Convert.ToDouble(Txtbx_ClaimValue.Text) == (double)0)
{
    Lbl_Message.Text = "Error. Claim Value ?";
    Txtbx_ClaimValue.Focus();
    return;
}







Can somebody please help me?





我尝试了什么:



我不知道什么是代码错误。



What I have tried:

I have no clue what's wrong with the code.

推荐答案

不要对用户输入使用转换操作:如果用户输入错误,它们会抛出异常。

相反,请使用TryParse:

Don't use Convert operations for user input: they throw an exception if the user enters something wrong.
Instead, use TryParse:
double d;
if (!double.TryParse(Txtbx_ClaimValue.Text, out d))
   {
   .. Report input problem to user do he can fix it ...
   return;
   }
if (d == 0.0)
   {
   Lbl_Message.Text = "Error. Claim Value ?";
   Txtbx_ClaimValue.Focus();
   return;
   }


这篇关于错误输入字符串的格式不正确。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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