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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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
SolutionsCSCommented:
Exactly right.  He should implement it ass addOrder maybe.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.