C# “must declare a body because it is not marked abstract, extern, or partial”
老实说,我不知道我为什么会犯这个错误。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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; } } } |
我也试过做一个实际的财产:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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; } } } |
建议?
试试这个:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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; } } } |
您需要为财产的
我怀疑你想这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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; } } } |
也就是说,我也会考虑简化这段代码。对于无效的输入,最好使用异常而不是属性设置器内部的消息框,因为它不会将您绑定到特定的UI框架。
如果这不合适,我建议将其转换为方法,而不是使用属性设置器。这一点尤其正确,因为属性具有"轻量级"的隐式期望,并且向用户显示消息框确实违反了该期望。
使用自动属性时,不能为setter提供自己的实现。换句话说,您应该这样做:
1 | public int Hour { get;set;} // Automatic property, no implementation |
或者为getter和setter提供您自己的实现,从您的示例中可以看出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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; } } } |
您需要为getter和setter提供一个主体,或者两者都不提供。由于您的setter中有非常重要的逻辑,所以您需要一个手动实现的getter,如下所示:
1 | get { return _hour; } |
如果您决定不需要setter中的逻辑,可以使用自动实现的属性,例如:
1 | public int Hour { get; set; } |
如果希望自动编译器提供基本实现,则不必为getter和setter提供主体。
但是,这并不要求您通过将web.config更新为类似于
1 2 3 4 5 6 | <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> |
您只需使用keywork值即可完成此操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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; } } } |
我收到了同样的错误消息,因为我有一个函数的参数名为保留字。
1 | public int SaveDelegate(MyModel.Delegate delegate) |
重命名变量委托解决了问题。