C#--An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in c#

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in c#

  • Monday, February 11, 2008 6:04 AM Ramyanaidu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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 = (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

  • Monday, February 11, 2008 8:28 AM rauhanlinnake Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
      Answer
    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 Snippet

    private 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 Big Smile

    Code Snippet

    public 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

  • Monday, February 11, 2008 8:28 AM rauhanlinnake Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
      Answer
    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 Snippet

    private 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 Big Smile

    Code Snippet

    public 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;

  • }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值