# 在一条语句中能否判断是否为空值并赋0

0
[已解决问题] 解决于 2013-01-29 21:18

if (Convert.ToDouble(txtfirstjdjes.Text) + Convert.ToDouble(txtsecondlyjdjes.Text) + Convert.ToDouble(txtthirdjdjes.Text) + Convert.ToDouble(txtfourjdjes.Text) > Convert.ToDouble(lblysjes))

Ole_Yao | 菜鸟二级 | 园豆：236

0

var first = string.IsNullOrEmpty(txtfirstjdjes.Text)?0:Convert.ToDouble(txtfirstjdjes.Text);

var second= string.IsNullOrEmpty(txtsecondjdjes.Text)?0:Convert.ToDouble(txtsecondjdjes.Text);

var third = string.IsNullOrEmpty(txtthirdjdjes.Text)?0:Convert.ToDouble(txtthirdjdjes.Text);

var four= string.IsNullOrEmpty(txtfourjdjes.Text)?0:Convert.ToDouble(txtfourjdjes.Text);

var ysjes= string.IsNullOrEmpty(lblysjes)?0:Convert.ToDouble(lblysjes);

if (first+second+third+four>ysjes)

Rich.T | 老鸟四级 |园豆：3440 | 2013-01-29 15:40

public static double ToDouble(this string str)

{

return string.IsNullOrEmpty(str)?0:Convert.ToDouble(str);

}

Rich.T | 园豆：3440 (老鸟四级) | 2013-01-29 15:42

@Rich.T: 能不能用完以后再变回来呢，有点无奈这种需求。。。

Ole_Yao | 园豆：236 (菜鸟二级) | 2013-01-29 15:58

@Ole_Yao:

Rich.T | 园豆：3440 (老鸟四级) | 2013-01-29 15:59

@Rich.T: 哦，没事，现在我添加了方法，但是报了很多莫名其妙的错

Ole_Yao | 园豆：236 (菜鸟二级) | 2013-01-29 16:18

@Rich.T: 把扩展的方法注释掉就不报错了，什么原因啊，大哥

Ole_Yao | 园豆：236 (菜鸟二级) | 2013-01-29 16:32

@Ole_Yao:

Rich.T | 园豆：3440 (老鸟四级) | 2013-01-29 16:34

@Rich.T: 那个已经好了，但是在判断中报错：输入字符串的格式不正确。这可能是什么原因啊

Ole_Yao | 园豆：236 (菜鸟二级) | 2013-01-29 16:53

@Ole_Yao:

Rich.T | 园豆：3440 (老鸟四级) | 2013-01-29 16:55

0

txtfirstjdjes.Text==null?"0":txtfirstjdjes.Text;

vancegf | 园豆：234 (菜鸟二级) | 2013-01-29 15:38
0

``` //委托
Func<string, double> toDouble = s =>
{
double d = 0d;

if (!string.IsNullOrEmpty(s))
double.TryParse(s, out d);

return d;
};
if (toDouble(txtfirstjdjes.Text) +
toDouble(txtsecondlyjdjes.Text) +
toDouble(txtthirdjdjes.Text) +
toDouble(txtfourjdjes.Text) > toDouble(lblysjes))```
Qlin | 园豆：2403 (老鸟四级) | 2013-01-29 15:39

@ifecy:

0

double num1=string.isnullorempty(txtfirstjdjes.Text.trim())?0:convert.todouble(txtfirstjdjes.Text.trim())

double num2=string.isnullorempty(txtsecondlyjdjes.Text.trim())?0:convert.todouble(txtsecondlyjdjes.Text.trim())

double num3=string.isnullorempty(txtthirdjdjes.Text.trim())?0:convert.todouble(txtthirdjdjes.Text.trim())

double num4=string.isnullorempty(txtfourjdjes.Text.trim())?0:convert.todouble(txtfourjdjes.Text.trim())

chenping2008 | 园豆：9836 (大侠五级) | 2013-01-29 15:57
0

``` 1  public static class Objects
2     {
3         /// <summary>
4         /// 将对象安全的转换为int类型（如果转换失败，则为默认值 true:1 false:0）
5         /// </summary>
6         /// <param name="o"></param>
7         /// <param name="defaultValue">默认值</param>
8         /// <returns></returns>
9         public static int ToSafeInt32(this object o, int defaultValue)
10         {               int num = defaultValue;
16             if (o.GetType() == Type.GetType("System.Boolean"))
17             {
18                 return (Convert.ToBoolean(o) ? 1 : 0);
19             }
20             try
21             {
22                 num = Convert.ToInt32(o);
23             }
24             catch
25             {
26             }
27             return num;
28         }　　}```

@ifecy: 这玩意真得是object类型的。 你要说从datatable 搞出一些数据，那全都是object类型的

您需要登录以后才能回答，未注册用户请先注册