Stack overflow error-Unhandled exception

This is the error which I am getting in small piece of my code:

An unhandled exception of type 'System.StackOverflowException' occurred in App_Code.wyq0lbyu.dll


What should I do?I need some workaround for this.

Thanks!

public class OrderCollection : List<Order>
        {
            public void Add(Order order)
            {
                this.Add(order);
            }
        }

Open in new window

onebite2Asked:
Who is Participating?
 
SolutionsCSConnect With a Mentor Commented:
Exactly right.  He should implement it ass addOrder maybe.
0
 
p_davisCommented:
need more information.-- where does it get called from, how is it used? are you in an infinite loop?
0
 
mastooCommented:
So what are you trying to do?  You have the Add function calling itself recursively, so it will just do that over and over until the stack runs out of room.  The fix is either don't have it call itself or have some condition that tells it to stop calling itself.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
SolutionsCSCommented:
He he he, common mistake.  You implement a list, so the functionality are in the base class.
Replace this with base

base.Add(order);
0
 
onebite2Author Commented:
Solution CS,


yeah changed it to bas.add and that erased the error.


Thanks!
0
 
SolutionsCSCommented:
Onebite2, can you accept my solutions since it work.  thank you
0
 
mastooCommented:
Can I point out that doesn't make much sense?  Add isn't virtual / overridable so you wouldn't alter the functionality.  With just the one line of code calling the base class method you'd be better off just getting rid of Add in your derived class (which also gets rid of the error message).
0
 
SolutionsCSCommented:
mastoo it's an implementation of a generics of type order.  A template so...  Instead of doing reflection  code this and run it in debug and observe what happen.  use of this.add call itself indefinitly till overflow. base.add to the real work.  It is logic and basic.
0
 
mastooCommented:
Yes, I got that part of it.  I'm saying it is bad to implement the derived class Add method since it is not declared as overridable in List<>.  It would be better to omit it since the base class Add is public anyway.
0
All Courses

From novice to tech pro — start learning today.