An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in c#
-
Hi
I have classes property and sketch in htis property will be linked to skecth class
and foure different classes which r linked to the skecth class
and two generic list one is for skecth and other is for data of type Object
when i run the program i iam grtting the error herepublic Sketch FirstData()
{
// return Datas[Datas.Count];foreach (Object data in Datas)
{
if (data is Door)
{
Door door = (Door)data;
}
if (data is Fixture)
{
Fixture fixture = (Fixture)data;
}
if (data is Wall)
{
Wall wall = (Wall)data;
}
if (data is Window)
{
Window window = (Window)data;
}
//return
}
return getSketch(1);
}private Sketch getSketch(Int32 id)
{
foreach (Object data in Datas)
{
if (data is Door)
{
Door door = (Door)data;
door.Id = id;
}
if (data is Fixture)
{
Fixture fixture = (Fixture)data;
fixture.Id = id;
}
if (data is Wall)
{
Wall wall = (Wall)data;
wall.Id = id;
}
if (data is Window)
{
Window window = (Window)data;
window.Id = id;
}
}
return getSketch(id);
}An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
how can i slove this problem?
can any one help me in this
Answers
-
Hi!
The answer is very simple: you are calling a function recursively over and over again, without letting it return which causes a stack overflow.
Code Snippetprivate Sketch getSketch(Int32 id)
{
....
return getSketch(id);
}
That is the line causing the method getSketch to be called over and over again. As you see, there is no way the code can get out of the function. I do not know what you are doing with the code, but you sure have to create a sketch object (what ever it is, and return it).
Here is a example what could be done to make the code work. Note that I do not know anything about the classes or your program
Code Snippetpublic Sketch FirstData()
{
return GetSketch(0); //Enough? Should it be zero rather that one? Collections are zero-based you know.
}
private Sketch GetSketch(int id)
{
//You have to create a sketch to be returned somewhere
Sketch sketch = new Sketch();
foreach (object data in Datas)
{
//TODO: What to do with the door, fixture, wall and window?
if (data is Door)
{
Door door = (Door)data;
door.Id = id;
//sketch.Add(door); //:D Just a guess
}
else if (data is Fixture) //Use else-if's, because data object cannot be different things at a time
{
Fixture fixture = (Fixture)data;
fixture.Id = id;
//sketch.Add(fixture); //:D Just a guess
}
else if (data is Wall)
{
Wall wall = (Wall)data;
wall.Id = id;
//sketch.Add(wall); //:D Just a guess
}
else if (data is Window)
{
Window window = (Window)data;
window.Id = id;
//sketch.Add(window); //:D Just a guess
}
}
//You have to return an object instead calling the same function over and over again
return sketch;
}
All Replies
-
Hi!
The answer is very simple: you are calling a function recursively over and over again, without letting it return which causes a stack overflow.
Code Snippetprivate Sketch getSketch(Int32 id)
{
....
return getSketch(id);
}
That is the line causing the method getSketch to be called over and over again. As you see, there is no way the code can get out of the function. I do not know what you are doing with the code, but you sure have to create a sketch object (what ever it is, and return it).
Here is a example what could be done to make the code work. Note that I do not know anything about the classes or your program
Code Snippetpublic Sketch FirstData()
{
return GetSketch(0); //Enough? Should it be zero rather that one? Collections are zero-based you know.
}
private Sketch GetSketch(int id)
{
//You have to create a sketch to be returned somewhere
Sketch sketch = new Sketch();
foreach (object data in Datas)
{
//TODO: What to do with the door, fixture, wall and window?
if (data is Door)
{
Door door = (Door)data;
door.Id = id;
//sketch.Add(door); //:D Just a guess
}
else if (data is Fixture) //Use else-if's, because data object cannot be different things at a time
{
Fixture fixture = (Fixture)data;
fixture.Id = id;
//sketch.Add(fixture); //:D Just a guess
}
else if (data is Wall)
{
Wall wall = (Wall)data;
wall.Id = id;
//sketch.Add(wall); //:D Just a guess
}
else if (data is Window)
{
Window window = (Window)data;
window.Id = id;
//sketch.Add(window); //:D Just a guess
}
}
//You have to return an object instead calling the same function over and over again
return sketch;
-
}