C# "must declare a body because it is not marked abstract, extern, or partial"

标签 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;
        }
    }
}

我也试过只做一个实际的属性(property):

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;
        }
    } 
}

建议?

最佳答案

试试这个:

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;
        }
    }
}

关于C# "must declare a body because it is not marked abstract, extern, or partial",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4810415/

相关文章:

c# - session 状态 MVC3

c# - 在上传 HTML5 vid 服务器端实现转换

c# - 在 Entity Framework 中将 tinyint 字段表示为枚举

c# - .net 中 Sha256 的更新功能在哪里?

c# - Task.Run() 是否与创建 Task 实例然后 Start() 相同?

c# - 自定义对象序列化与 PreserveReferencesHandling

c# - 使用 C# 反序列化 JSON(多个对象)

c# - 接收 HTTP 响应时发生错误

c# - 停止 WebBrowser 会导致打开 Internet Explorer

c# - 删除文件夹 'bin' 时出错。系统调用级别不正确