Document directory
An unhandled exception of Type 'System. stackoverflowexception 'occurred in mscorlib. dll in C #
- Monday, February 11,200 amramyanaidu0
Sign in to vote
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 here
Public sketch firstdata ()
{
// Return datas [datas. Count];
Foreach (Object Data in datas)
{
If (data is 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) 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
- Monday, February 11,200 amrauhanlinnake0
Sign in to votehi!
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 cocould 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? Shocould 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) 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
- Monday, February 11,200 amrauhanlinnake0
Sign in to votehi!
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 cocould 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? Shocould 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) 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;
-
}