2011-01-26 29 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; 
     } 
    } 
} 
相关问题