【问题标题】:System.StackOverflowExceptionSystem.StackOverflowException
【发布时间】:2009-11-04 09:11:32
【问题描述】:

请帮我解决 System.StackOverflowException 我正在设计一个 .aspx 将记录写入数据库我使用 4 层架构来实现这一切都在工作,但是当我编译页面时,它会显示插入数据的字段,当我将数据插入这些字段并单击提交按钮,然后显示 System.StackOverflowException 发生

public class Customers
{
    public Customers()
    {
        int CustomerID = 0;
        string Fname = string.Empty;
        string Lname = string.Empty;
        string Country = string.Empty;
    }
    public int CustomerID
    {
        get { return CustomerID; }
        set { CustomerID = value; }
    }
    public string Fname
    {
        get { return Fname; }
        set { Fname = value; }****
    }
    public string Lname
    {
        get { return Lname; }
        set { Lname = value; }
    }
    public string Country
    {
        get { return Country; }
        set { Country = value; }
    }

当页面正在执行时,会弹出一个窗口并显示 System.StackOverflowException,请给我任何解决此问题的方法

【问题讨论】:

  • 请尝试使用 Markdown 格式化您问题中的代码,以便我们阅读。
  • 另外,我在代码中看不到任何会导致堆栈溢出的内容,所以请您发布您的异常堆栈跟踪吗?
  • 请向我们提供您在点击提交按钮时使用的代码。另外,请重新格式化您的帖子(代码)。

标签: c# asp.net .net stack-overflow c#-2.0


【解决方案1】:
public int CustomerID
{
    get { return CustomerID; }
    set { CustomerID = value; }
}

您正在递归地将值分配给自身。其他属性也是如此。

您需要用另一个名称定义一个备份字段,例如:

private int _CustomerId;
public int CustomerID
{
    get { return _CustomerID; }
    set { _CustomerID = value; }
}

甚至更好:

public int CustomerId {get; set;}

【讨论】:

    【解决方案2】:

    尝试以下方法:

    public class Customers
    {
    
        private int _CustomerID; 
        private string _Fname;
        private string _Lname;
        private string _Country;
    
        public Customers()    
        {        
            int CustomerID = 0;        
            string Fname = string.Empty;        
            string Lname = string.Empty;        
            string Country = string.Empty;    
        }    
    
        public int CustomerID    
        {        
            get { return _CustomerID; }        
            set { _CustomerID = value; }    
        }    
    
        public string Fname    
        {        
            get { return _Fname; }        
            set { _Fname = value; }
        }    
    
        public string Lname    
        {        
            get { return _Lname; }        
            set { _Lname = value; }    
        }    
    
        public string Country    
        {        
            get { return _Country; }        
            set { _Country = value; }    
        }
    

    【讨论】:

      【解决方案3】:

      
          public Customers()
          {
              int CustomerID = 0;
              string Fname = string.Empty;
              string Lname = string.Empty;
              string Country = string.Empty;
          }
      public int CustomerID { get; set; }
      public string Fname { get; set; }
      public string Lname { get; set; }
      public string Country { get; set; }
      }

      【讨论】:

      • @Kobi 当你使用 VS2008 时会这样(甚至针对 .NET 2.0)
      【解决方案4】:

      在您的属性 Get & Set 中有无限的 recusive 调用,例如:

      string Lname{ 
          get { return Lname; } 
          set { Lname = value; }
      } 
      

      Lname = 值;将一次又一次地致电您的住宿。

      【讨论】:

        猜你喜欢
        • 2018-05-26
        • 2020-12-08
        • 1970-01-01
        • 1970-01-01
        • 2011-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-27
        相关资源
        最近更新 更多