【问题标题】:UserControl TextBox get set PropertyUserControl TextBox 获取设置属性
【发布时间】:2013-03-08 07:22:10
【问题描述】:

我收到以下错误:

 An unhandled exception of type 'System.StackOverflowException'
 occurred in ciscontrols.dll

以下是相关代码:

private int Dval;
public int DecPlaces
{
    get { return Dval; }
    set
    {
        DecPlaces = value;
        if (value < 2)
        {
            throw new ArgumentOutOfRangeException("decplaces", "decimal places minimum value should be 2.");
        }
        else this.Dval = value;
    }
}

【问题讨论】:

  • DecPlaces = value 应该做什么?

标签: c# winforms user-controls get set


【解决方案1】:

在代码中查看我的评论 -

private int Dval;
    public int DecPlaces
    {
        get { return Dval; }
        set
        {
            //DecPlaces = value;  **** This is calling set method again, hence the exception. Just comment this line

            if (value < 2)
            {
                throw new ArgumentOutOfRangeException("decplaces", "decimal places minimum value should be 2.");
            }
            else this.Dval = value;
        }
    }

【讨论】:

    【解决方案2】:

    您正在调用 Set Property Infinite Manner

      DecPlaces = value;
    

    使用一些 lcoal 变量来做到这一点。

    int m= value;
    

    【讨论】:

      【解决方案3】:

      这是你的问题:

      DecPlaces = value;
      

      你是在自我引用。你一直打电话给你的二传手。只需删除该行,您应该会很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多