※ 引述《apologize (人生在世很愜意)》之銘言:
: checkBox1.Checked == true ? timer1.Enabled = true : timer1.Enabled = false;
: 我是這樣寫,可是他說只能用陳述式表示,
: 可是不是要判別式才能用?請問要怎樣修改?
MSDN ?: 運算子 (C# 參考)
http://msdn.microsoft.com/zh-tw/library/ty67wk28.aspx
語法
condition ? first_expression : second_expression;
因為expression 實際上是回傳給這個語法的值
e.g
int a = (true ? 0 : 1);//合法
int b = (false ? "0" : "1");//非法,因為b是int,但expression是string
所以你的程式應該寫成
timer1.Enabled = (checkBox1.Checked ? true : false);
或者
if(checkBox1.Checked)
{
timer1.Enabled = true;
}
else
{
timer1.Enabled = false;
}
若這樣寫也可以正常賦值,但無意義
bool tmp = checkBox1.Checked ? timer1.Enabled = true : timer1.Enabled = false;