2016-07-12 89 views
-2
public object Value 
{ 
    get 
    { 
     if (this.realDate) 
      return (object)base.Value; 
     return (object)DBNull.Value; 
    } 
    set 
    { 
     if (Convert.IsDBNull(value)) 
     { 
      this.realDate = false; 
      this.oldFormat = this.Format; 
      this.Format = DateTimePickerFormat.Custom; 
      this.CustomFormat = " "; 
     } 
     else 
     { 
      this.realDate = true; 
      // the line below is erroneous 
      this.Value = Convert.ToDateTime(value); 
     } 
    } 
} 

类型“System.StackOverflowException”未处理的异常发生在application.exe我是无能为什么发生这种情况类型“System.StackOverflowException”未处理的异常发生在application.exe

+2

什么'返回(对象)base.Value;'做?我的猜测是它再次调用相同的访问器。尽管我们不能没有[mcve]。并且你的二传手肯定会自己调用非空值... –

回答

4
public object Value 
{ 
    … 
    set 
    { 
     this.Value = value; 
    } 
} 

这实质上会再次调用Value的setter。所以你从setter的setter中调用setter的setter ...导致一个由StackOverflowException停止的无限循环。

你应该有一个支持字段你写你的价值,例如,像这样:

private object _value; 

public object Value 
{ 
    get { return _value; } 
    set 
    { 
     // some logic 
     _value = value; 
    } 
} 
+0

非常感谢你....它像魅力一样工作.. –

+0

@saqibali不客气!请记住[接受答案](http://meta.stackexchange.com/a/5235/141542),如果它解决了您的问题,将此问题标记为已解决。 – poke

0

您有一个你的价值getter中无尽的递归调用:

(object)base.Value; 

这将导致计算器

相关问题