错误堆栈溢出错误 [英] Error stack over flow error

查看:121
本文介绍了错误堆栈溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-----第1栏----- -----第2栏-----

__________________________________________

|重新命名|第1行

|名字:Rajesh |姓氏:Patil |第2行

|日期:15/07/1947 |性别:男|第3行





如上图所示。我想根据用户需要动态设计表格。

现在以上面的表2列表为例。



对于Column,将Class编写为Columns.cs,如下所示

-----Column 1----- -----Column 2-----
__________________________________________
| Repot Name | Row 1
|First Name:Rajesh | Last Name :Patil| Row 2
|Date:15/07/1947 | Gender:Male| Row 3


As i shown above image . i want to design table dynamically as user required.
Now take example of above table 2 column table .

For Column is written Class as Columns.cs as below

public class Columns
    {
        public List<Columns> rCol { set; get; }
        Columns mCurrentColumn1;
        public Columns()
        {
            rCol = new List<Columns>();
        }
       
        public string   ColName       { set; get; }
        public string   Value         { set; get; }
        
        public Columns AddColumn(string ColName, String Value)
        {
            mCurrentColumn1 = new Columns();
            mCurrentColumn1.ColName = ColName;
            mCurrentColumn1.Value = Value;
            rCol.Add(mCurrentColumn1);

            return mCurrentColumn1;
        }
    }
	
For AddColumn is written Class as AddColumn.cs as below
	
public class AddColumn
    {
        public List<AddColumn> rCol1 { set; get; }
        AddColumn mCurrentColumn2;

        public AddColumn()
        {
            rCol1 = new List<AddColumn>();
        }
        public void AddCol(Columns AddCol)
        {
            mCurrentColumn2 = new AddColumn();
            mCurrentColumn2.AddCol(AddCol);
            rCol1.Add(mCurrentColumn2);
        }
    }	
	
For AddRow is written Class as AddRow.cs as below
	
public class AddRow
    {
        public List<AddRow> rCol2 { set; get; }
        AddRow mCurrentColumn3;

        public AddRow()
        {
            rCol2 = new List<AddRow>();
        }

        public void AddColumn(AddColumn AddCol)
        {
            mCurrentColumn3 = new AddRow();
            mCurrentColumn3.AddColumn(AddCol);
            rCol2.Add(mCurrentColumn3);
        }
    }	
	
	

public partial class Default : System.Web.UI.Page
{
	Columns Col = new Columns();
	AddColumn ACol = new AddColumn();
	AddRow ARow = new AddRow();	
	protected void Page_Load(object sender, EventArgs e)
	{
		Col.AddColumn("MainHeader1", "State Bank of India");
		Col.AddColumn("MainHeader2", "State Bank of India");
		Col.AddColumn("MainHeader3", "State Bank of India");

		ACol.AddCol(Col);
		ARow.AddColumn(ACol);
	}	
}





当我在AddRow.cs类中运行程序时显示错误为

类型'System.StackOverflowException'的未处理异常发生在



我尝试过:





When i run program in AddRow.cs class showing error as
"An unhandled exception of type 'System.StackOverflowException' occurred in"

What I have tried:

public partial class Default : System.Web.UI.Page
{
	Columns Col = new Columns();
	AddColumn ACol = new AddColumn();
	AddRow ARow = new AddRow();	
	protected void Page_Load(object sender, EventArgs e)
	{
		Col.AddColumn("MainHeader1", "State Bank of India");
		Col.AddColumn("MainHeader2", "State Bank of India");
		Col.AddColumn("MainHeader3", "State Bank of India");

		ACol.AddCol(Col);
		ARow.AddColumn(ACol);
	}	
}

推荐答案

这个类是递归的:

This class is recursive:
public class AddRow
{
    public List<AddRow> rCol2 { set; get; }
    AddRow mCurrentColumn3;
    
    public AddRow()
    {
        rCol2 = new List<AddRow>();
    }
    
    public void AddColumn(AddColumn AddCol)
    {
        mCurrentColumn3 = new AddRow();
        mCurrentColumn3.AddColumn(AddCol);
        rCol2.Add(mCurrentColumn3);
    }
}



该类有一个类类型的成员变量( mCurrentColumn3 )。当类具有构造函数时(初始构造函数调用成员的构造函数,调用成员的构造函数等),这将不起作用。



没有构造函数或它什么都不做,错误是通过在 AddColumn() mCurrentColumn3.AddColumn()来获取的>再次调用函数本身的函数。



两者都会继续无限,但在堆栈溢出时会停止。



解决方案:重新思考你的班级设计。


The class has a member variable of the class type (mCurrentColumn3). This won't work when the class has a constructor (the initial constructor calls the constructor of the member which calls the constructors of the members and so on).

When there is no constructor or it does nothing, the error is sourced by calling mCurrentColumn3.AddColumn() in the AddColumn() function which calls the function itself again.

Both would continue infinite but are stopped when the stack overflows.

Solution: Rethink your class design.


你有两个地方会发生这种情况。在AddColumn.AddCol方法和AddRow.AddColumn方法中 - 两者都是出于同样的原因。让我们看一下AddColumn.AddCol方法:

You have 2 places where this will occur. In your AddColumn.AddCol method, and your AddRow.AddColumn method - and both for the same reason. Lets just look at the AddColumn.AddCol method:
public void AddCol(Columns AddCol)
{
    mCurrentColumn2 = new AddColumn();
    mCurrentColumn2.AddCol(AddCol);
    rCol1.Add(mCurrentColumn2);
}





第一行创建一个新的AddColumn。

第二行然后调用AddCol然后调用AddCol

   第一行创建一个新的AddColumn

    2nd Line然后调用AddCol然后

  ;     第一行创建一个新的AddColumn



等等 - 直到堆栈填满了太多的调用,并且错误生成。 AddRow.AddColumn方法完全相同。



First line creates a new AddColumn.
2nd Line then calls AddCol which then
   First line creates a new AddColumn
   2nd Line then calls AddCol which then
      First line creates a new AddColumn

and so on - until the stack fills up with too many calls, and the error is generated. The AddRow.AddColumn method does exactly the same thing.


这篇关于错误堆栈溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆