Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form Class, Keep getting a "System.StackOverflowException" on a simple code test

I have been trying to implement and adapt my coding style moving form more of a procedural programming style but have been running into problems, when trying to run another class working with the systems registry. Furthermore the code works fine in the main class. P.S I come form a scripting background.

Code Error: An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

I have cut down the code from the main application to make it easier to find the problem.

Main Form Class:

public partial class Form1test : Form
{
    // An unhandled exception of type 'System.StackOverflowException' 
    // occurred in System.Windows.Forms.dll
    public Form1test() 
    {
        TestClass lsr = new TestClass();
        lsr.chkRegAct();
        InitializeComponent();
    }  
}

TestClass:

class TestClass : Form1test
{
   //rest of code
}
like image 261
Mattlinux1 Avatar asked Sep 15 '26 00:09

Mattlinux1


1 Answers

TestClass is derived from Form1Test.

You create a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates a new TestClass which results to calling the constructor of base class (Form1Test) which generates .....

StackOverflowException due to lot of function calls caused by infinite recursion.

like image 183
Hamid Pourjam Avatar answered Sep 16 '26 21:09

Hamid Pourjam