Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
-----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
C#
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);
	}	
}


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

What I have tried:

C#
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);
	}	
}
Posted
Updated 17-Nov-16 23:01pm
v2
Comments
F-ES Sitecore 18-Nov-16 4:56am    
You get a stack overflow exception when your code enters an infinite loop like

void SomeFunction()
{
SomeFunction();
}

Step through your code line by line in the debugger and it should become clear where your infinite loop is.

This class is recursive:
C#
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);
    }
}

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.
 
Share this answer
 
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:
C#
public void AddCol(Columns AddCol)
{
    mCurrentColumn2 = new AddColumn();
    mCurrentColumn2.AddCol(AddCol);
    rCol1.Add(mCurrentColumn2);
}


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900