简体   繁体   中英

How can fix System.StackOverflow Exception?

I used 2 classes, and I need send/receive methods and variables. But when I make an instance of this class it gives me a System.StackOverflowException .

How can fix this problem?

This is my code:

class Setup1
{
   Setup2 set2 = new Setup();

   int a = 5;

   public int myMethod();
   {
      set2.b =  a + 10;
      return set2.b;
   }
}

class Setup2
{
    Setup1 set1 = new Setup();
    public int b = 0;
    void  Show()
    {
        MessageBox.Show(set1.myMethod());
    }

}

You have an infinite recursion.

In the constructor of Setup2() you call the constructor of Setup1() . There you call the constructor of Setup2() and so on, infinitely. Your memory runs out, and your stack overflows.

似乎您有循环构造函数调用,导致堆栈溢出异常。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM