我不確定爲什麼我得到這個錯誤是誠實的。C#「必須聲明一個身體,因爲它沒有標記爲抽象,外部或部分」
private int hour
{
get;
set
{
//make sure hour is positive
if (value < MIN_HOUR)
{
hour = 0;
MessageBox.Show("Hour value " + value.ToString() + " cannot be negative. Reset to " + MIN_HOUR.ToString(),
"Invalid Hour", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
//take the modulus to ensure always less than 24 hours
//works even if the value is already within range, or value equal to 24
hour = value % MAX_HOUR;
}
}
}
我也嘗試只是在做一個實際的屬性:
public int hour
{
get;
set
{
//make sure hour is positive
if (value < MIN_HOUR)
{
hour = 0;
MessageBox.Show("Hour value " + value.ToString() + " cannot be negative. Reset to " + MIN_HOUR.ToString(),
"Invalid Hour", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
//take the modulus to ensure always less than 24 hours
//works even if the value is already within range, or value equal to 24
hour = value % MAX_HOUR;
}
}
}
建議?
重複:http://stackoverflow.com/questions/1121940/automatic-properties-in-c-3-must-declare-a-body-for-get-if-i-declare-one- for-s – ChristopheD 2011-01-26 21:53:12
快速提示:在setter中顯示UI是一個非常糟糕的主意。 – driis 2011-01-26 21:54:07
如果你確實得到了;並設置;那麼它是一個自動屬性。如果你定義了其中一個,它不再是一個自動屬性。 – JDPeckham 2013-06-05 00:51:54