C #-An unhandled exception of Type 'System. stackoverflowexception 'occurred in msco

Source: Internet
Author: User
Tags mscorlib
Document directory
  • Answers
  • All replies

An unhandled exception of Type 'System. stackoverflowexception 'occurred in mscorlib. dll in C #

  • Monday, February 11,200 amramyanaidu

    0

    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

    • Reply
    • Quote
     
Answers
  • Monday, February 11,200 amrauhanlinnake

    0

    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 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 cocould be done to make the code work. Note that I do not know anything about the classes or your program

    Code snippet

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

    }

    • Reply
    • Quote
     
All replies
  • Monday, February 11,200 amrauhanlinnake

    0

    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 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 cocould be done to make the code work. Note that I do not know anything about the classes or your program

    Code snippet

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

  • }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.