0

我收到一个错误,例如

“在 ciscontrols.dll 中发生了‘System.StackOverflowException’类型的未处理异常”。

我的代码如下

    private int _vin;

    public int MaxLength
    {
        get { return _vin; }

        set //Here your answer solve the promblem
        {
            txtLocl.MaxLength = value;
            if (value < 2)
            {
                throw new ArgumentOutOfRangeException("MaxLength","MaxLength MinValue should be 2.");
            }
            else this._vin = value;
        }
    }

我正在为小数位创建一个新属性

private int Dval;
    public int DecPlaces
    {
        get { return Dval; }
        set// here it showing the same error
        {
            DecPlaces = value; // MaxLength is a preDefined Property but  DecPlaces created by me.
            if (value < 2)
            {
                throw new ArgumentOutOfRangeException("decplaces", "decimal places minimum value should be 2.");
            }
            else this.Dval = value;
        }
    }
4

3 回答 3

2

你的 setter 是递归的,因为你有

this.MaxLength = value;

在你的二传手内。这将导致无限循环,并最终导致StackOverflowException.

采用

this._vin = value;

反而

于 2013-03-08T03:50:06.013 回答
2

您的属性的设置器在此行调用自身:

 this.MaxLength = value;

将其更改为:

set //Here i am getting Error
{
    txtLocl.MaxLength = value;
    if (value < 2)
    {
        throw new ArgumentOutOfRangeException("MaxLength","MaxLength MinValue should be 2.");
    }
    else this._vin = value;
}

For this particular exception, the Call Stack window displayed in Visual Studio while debugging is helpful in seeing which methods are calling each other in an infinite loop. The setter of a property eventually becomes a method called set_MaxLength in the run-time.

于 2013-03-08T03:50:57.230 回答
1
this.MaxLength = value

这一行触发了一个无限循环,因为它调用了set你已经在其中的访问器。你的意思是设置一个支持变量的值吗?

于 2013-03-08T03:50:20.017 回答