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

前端 未结 7 566
轻奢々
轻奢々 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:23

    You DO NOT have to provide a body for getters and setters IF you'd like the automated compiler to provide a basic implementation.

    This DOES however require you to make sure you're using the v3.5 compiler by updating your web.config to something like

     <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 讨论(0)
  • 2020-12-15 15:27

    Try this:

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 15:28

    You need to provide a body for the get; portion as well as the set; portion of the property.

    I suspect you want this to be:

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

    That being said, I'd also consider making this code simpler. It's probably is better to use exceptions rather than a MessageBox inside of your property setter for invalid input, as it won't tie you to a specific UI framework.

    If that is inappropriate, I would recommend converting this to a method instead of using a property setter. This is especially true since properties have an implicit expectation of being "lightweight"- and displaying a MessageBox to the user really violates that expectation.

    0 讨论(0)
  • 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;
            }
         }
    }
    
    0 讨论(0)
  • 2020-12-15 15:29

    You need to either provide a body for both the getter and setter, or neither. Since you have non-trivial logic in your setter, you need a manually-implemented getter like so:

    get { return _hour; }
    

    If you decide you don't need the logic in the setter, you could go with an automatically-implemented property like so:

    public int Hour { get; set; }
    
    0 讨论(0)
  • 2020-12-15 15:36

    You can just use the keywork value to accomplish this.

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题