i have 2 forms. one of them is a form that User select a school class so click OK. I use
public int[] grad_class=new int[2];
for save which School class has selected. (list of classes is COMBO BOX)
after that i open another new form to edit information of that class’ students.
i use Switch like this:
switch(grad_class)
{
case "11": something to do..., break;
case "12": something to do..., break;
}
But My problem :
I define “grad_class” in form No.1 . so when i want to use that i have to do this:
form1 f1 = new form1();
f1.grade_class;
and in form No.2 i call this code.
in form No.1 i call:
form2 f2 = new form2();
OCCUR STACK OVERFLOW EXCEPTION !!!
You state:
Well, there’s your issue right there. You state that in
form1you callform2.ctorand inform2you callform1.ctor. So your code looks like this:Well, of course this results in a stack overflow. These are mutally recursive functions. You have no return condition, so you exhaust your stack space. Quite simply, what is happening is that the machine has to store state about where to go next when a method is finished. It typically stores this information in space called the stack. The stack is bounded though, so if you push too many “this is where to go next” blocks onto the stack by repeatedly invoking methods, you will exhaust this stack space and hit the infamous stack overflow exception.
Edit: You said:
Effectively what I suspected. You have a field initializer in
ChooseGradethat instantiates a new instance ofEditStudent. ButEditStudenthas a field initializer that instantiates a new instance ofChooseGrade. So when you instantiate a new instance ofChooseGrade, this causes the constructor forEditStudentto be invoked, which causes the constructor forChooseGradeto be invoked, which causes the constructor forEditStudentto be invoked, and on and on it goes until you overflow your stack.