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

前端 未结 7 589
轻奢々
轻奢々 2020-12-15 15:05

I\'m not sure why i\'m getting this error to be honest.

private int hour
{
    get;
    set
    {
        //make sure hour is positive
        if (value <         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 15:29

    You cannot provide your own implementation for the setter when using automatic properties. In other words, you should either do:

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

    or provide your own implementation for both the getter and setter, which is what you want judging from your example:

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

提交回复
热议问题