2011-01-26 70 views
40

我不確定爲什麼我得到這個錯誤是誠實的。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; 
     } 
    } 
} 

建議?

+0

重複: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

+32

快速提示:在setter中顯示UI是一個非常糟糕的主意。 – driis 2011-01-26 21:54:07

+8

如果你確實得到了;並設置;那麼它是一個自動屬性。如果你定義了其中一個,它不再是一個自動屬性。 – JDPeckham 2013-06-05 00:51:54

回答

28

試試這個:

private int hour; 
public int Hour 
{ 
    get { return hour; } 
    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; 
     } 
    } 
} 
22

使用自動屬性時,無法爲setter提供自己的實現。換句話說,您應該做的:

public int Hour { get;set;} // Automatic property, no implementation 

提供自己實現的getter和setter這兩個,這是你想從你的例子來看什麼:

public int Hour 
{ 
    get { return hour; } 
    set 
    { 
     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; 
     } 
    } 
} 
23

您需要爲get;部分提供身體還有PROPERT的部分年。

我懷疑你希望這是:

private int _hour; // backing field 
private int Hour 
    { 
     get { return _hour; } 
     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; 
      } 
     } 
    } 

話雖這麼說,我還考慮使這個代碼更簡單。在屬性設置器中使用異常而不是MessageBox可能更好,因爲它不會將您綁定到特定的UI框架。

如果這不合適,我建議將其轉換爲一種方法,而不是使用屬性設置器。這是特別真實的,因爲屬性具有「輕量級」的隱含期望 - 並且向用戶顯示MessageBox確實違反了期望。

7

您需要爲getter和setter提供一個正文,或者都不要。既然你在二傳手有不平凡的邏輯,你需要手動實現吸氣,像這樣:

get { return _hour; } 

如果您決定不需要在二傳手的邏輯,你可以用一個自動實現的去屬性如下:

public int Hour { get; set; } 
5

如果您希望自動編譯器提供基本實現,則不必爲getter和setter提供一個主體。

這確實需要但是你要確保你使用的V3.5編譯器更新您的web.config像

<compilers> 
    <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"> 
    <providerOption name="CompilerVersion" value="v3.5"/> 
    <providerOption name="WarnAsError" value="false"/> 
    </compiler> 
</compilers> 
0

您可以只使用keywork值來實現這一點。

public int Hour { 
    get{ 
     // Do some logic if you want 
     //return some custom stuff based on logic 

     // or just return the value 
     return value; 
    }; set { 
     // Do some logic stuff 
     if(value < MINVALUE){ 
      this.Hour = 0; 
     } else { 
      // Or just set the value 
      this.Hour = value; 
     } 
    } 
} 
相關問題