C#“必须声明一个正文,因为它没有标记为抽象,外部或部分”

时间:2011-01-26 21:48:43

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

建议?

8 个答案:

答案 0 :(得分:32)

试试这个:

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 :(得分:25)

您需要为get;部分以及该属性的set;部分提供正文。

我怀疑你想要这个:

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确实违反了该期望。

答案 2 :(得分:24)

使用自动属性时,无法为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;
        }
     }
}

答案 3 :(得分:8)

你需要为getter和setter提供一个body,或者两者都不提供。由于你的setter中有非平凡的逻辑,你需要一个手动实现的getter,如下所示:

get { return _hour; }

如果你决定不需要setter中的逻辑,你可以使用自动实现的属性,如下所示:

public int Hour { get; set; }

答案 4 :(得分:5)

如果您希望自动编译器提供基本实现,则不必为getter和setter提供正文。

然而,这需要您确保通过将web.config更新为

来使用v3.5编译器。
 <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>

答案 5 :(得分:1)

您可以使用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;
        }
    }
}

答案 6 :(得分:0)

我得到了相同的错误消息,因为我有一个函数,该函数的参数名为保留字。

   public int SaveDelegate(MyModel.Delegate delegate)

重命名变量委托可以解决此问题。

答案 7 :(得分:-1)

must declare a body because it is not marked abstract, extern, or partial我在这里遇到了同样的问题:

    private static void swapMth(ref int x, ref int y);
    {
        int num = x;
        x = y;
        y = num;
    }


    private void button_Click(object sender, EventArgs e)
    {
        int x = 10;
        int y = 20;
        labelResult.Text = $"Befor      n1 = {x} , n2={y} ";
        swapMth(ref x, ref y);
        labelResult.Text += $"\n After  n1 = {x} , n2={y}";
    }

并通过删除“;”解决了从方法行:

private static void swapMth(ref int x, ref int y);问题

private static void swapMth(ref int x, ref int y) 已解决

我知道这是基本错误,希望有人可以通过此注释得到帮助。

    private static void swapMth(ref int x, ref int y)
    {
        int num = x;
        x = y;
        y = num;
    }


    private void button_Click(object sender, EventArgs e)
    {
        int x = 10;
        int y = 20;
        labelResult.Text = $"Befor      n1 = {x} , n2={y} ";
        swapMth(ref x, ref y);
        labelResult.Text += $"\n After  n1 = {x} , n2={y}";
    }