语言 C# 我现在要画面取得的值进行转换:画面 textbox转换成 Decimal 类型
Convert.ToDecimal(this.textbox1.Text.Trim())但是有个问题,我没有输入时候,转化报错,注:我不想一个一个的判断是否为空,画面的控件很多!!!
Convert.ToDecimal(this.textbox1.Text.Trim())但是有个问题,我没有输入时候,转化报错,注:我不想一个一个的判断是否为空,画面的控件很多!!!
{
if(c is TextBox)
{
TextBox tb=c as TextBox;
if(!string.IsNullOrEmpty(tb.Text)
{
//do some thing
}
}
}
public static class ExtendConvert
{
public static decimal MyConvert(this Convert c, string str)
{
if (str.Trim() == string.Empty)
{
return decimal.Zero;
}
return Convert.ToDecimal(str);
}
}用扩展方法吧,使用的时候直接调ExtendConvert.MyConvert而不再使用Convert.ToDecimal
public string isNull(string value)
{
if (!string.IsNullOrEmpty(value.Trim()))
{
return value.Trim();
}
else
{
return "";
}
}
public string isNull(decimal? value)
{
if (value != null)
{
return value.ToString();
}
else
{
return "";
}
}