UserControl TextBox获取set属性

时间:2013-03-08 07:22:10

标签: c# winforms user-controls get set

我收到以下错误:

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

3 个答案:

答案 0 :(得分:2)

在代码中查看我的评论 -

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

答案 1 :(得分:1)

您正在调用Set Property Infinite Manner

  DecPlaces = value;

使用一些局部变量来执行此操作。

int m= value;

答案 2 :(得分:0)

这是你的问题:

DecPlaces = value;

你是自我引用的。你一直打电话给你的二传手。只需删除该行就可以了。

相关问题