[英]How can fix System.StackOverflow Exception?
I used 2 classes, and I need send/receive methods and variables. 我使用了2个类,并且需要发送/接收方法和变量。 But when I make an instance of this class it gives me a
System.StackOverflowException
. 但是当我创建此类的实例时,它给了我
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()
. 在
Setup2()
的构造函数中,调用Setup1()
的构造函数。 There you call the constructor of Setup2()
and so on, infinitely. 在那里,您无限调用
Setup2()
的构造函数,依此类推。 Your memory runs out, and your stack overflows. 您的内存用完,并且堆栈溢出。
似乎您有循环构造函数调用,导致堆栈溢出异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.