这是导致问题的一行:
lblClimberInfo.Text = selectedClimber + "\r\n" + "Finished: " + selectedClimber.HasFinished ? "Yes" : "DNF" + "\r\n" + "Time: " + selectedClimber.Hours.ToString("00") + ":" + selectedClimber.Minutes.ToString("00") + ":" + selectedClimber.Seconds.ToString("00");
来自我的主要课程:
private bool hasFinished;
public bool HasFinished
{
get { return hasFinished; }
set { hasFinished = false; }
}
我希望它如果Finished: Yes
bool为真,则显示HasFinished
,如果Finished: DNF
bool为false,则显示HasFinished
并显示该值。在某一刻,它默认显示Finished: False
但是当我点击一个按钮来运行一个基本上改变bool的方法时,而不是说Finished: True
它只是说Finished:
。
我希望能够自定义它,而不是输出True
或False
答案 0 :(得分:1)
由于运算符优先级问题,请在三元运算符周围加上括号。否则,“时间”仅显示HasFinished
是否为假。
lblClimberInfo.Text = selectedClimber + "\r\n" + "Finished: " + (selectedClimber.HasFinished ?
"Yes" : "DNF") + "\r\n" + "Time: " + selectedClimber.Hours.ToString("00") + ":" +
selectedClimber.Minutes.ToString("00") + ":" + selectedClimber.Seconds.ToString("00");
另外,你的二传手很有意思:
private bool hasFinished;
public bool HasFinished
{
get { return hasFinished; }
set { hasFinished = false; }
}
似乎HasFinished不可能是真的,除非你在你班级的其他地方修改私有变量。但是,不是总是将属性设置为false的setter,最好省略setter,从而使属性为只读。您可能需要阅读Using Properties in C#
要定义正确的setter,请改为使用:
set { hasFinished = value; }