在一行上评估Property值与if else语句

时间:2014-08-05 12:22:08

标签: c# asp.net

在使用下面的代码时,我收到此错误(只有赋值,调用,递增,递减,等待和新对象表达式可用作语句)。

decimal _percentagefootervalue;

public decimal PercentageFooterValue 
{ 
    get 
    {
        return this._percentagefootervalue;
    }
    set
    {
        this._percentagefootervalue = value;
    }
}
......
//error here
PercentageFooterValue == 100 ? lblPercentText.ForeColor = System.Drawing.Color.Black : lblPercentText.ForeColor = System.Drawing.Color.Red;

现在,我绝对可以使用下面这段代码继续生活。然而,在网上查看后,我仍然无法理解我做错了什么,并且仍然想知道如果可能的话,它如何在一条线上工作。

//works
if (PercentageFooterValue == 100)
{
   lblPercentText.ForeColor = System.Drawing.Color.Black;
}
else
{
   lblPercentText.ForeColor = System.Drawing.Color.Red;
}

再次感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

lblPercentText.ForeColor = PercentageFooterValue == 100 ? System.Drawing.Color.Black : System.Drawing.Color.Red;

答案 1 :(得分:0)

语句是C#语言(任何语言)的基础结构。您的C#语句不正确。请参阅下面关于c#语句的msdn指南。

http://msdn.microsoft.com/en-us/library/ms173143.aspx

相关问题