System.StackOverflowException

时间:2009-11-04 09:11:33

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

请关于System.StackOverflowException帮助我 即时设计.aspx将记录写入数据库我使用4层结构来实现这一切都正常工作但是当我编译页面然后它显示字段插入数据,当我将数据插入那些字段并clik提交按钮然后显示出发生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,请给我任何解决此问题的方法

4 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分:1)

您的属性中有无限的递归调用Get&设置如下:

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

名称=值;将再次致电您的财产&试。

答案 2 :(得分:1)

尝试以下方法:

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 :(得分:1)


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