The requested operation caused a stack overflow

I am trying to create a derived class and the error pops up. Not quite sure how it comes out. Please help!

Base class:

public class Command : identifiableobject
{
    LookCommand l = new LookCommand();

    public virtual string Execute (Player p, string[] text)
    {
        return "";

    public Command (string[] ids) : base(ids)
    {
    }
}

Derived class

public class LookCommand : Command
{
    public LookCommand () : base (new string[] {"look"})
        {
        }
}

The error pops up when I try to create a new Command Object. Any ideas why?

Jon Skeet
people
quotationmark

This is the problem:

public class Command : identifiableobject
{
    LookCommand l = new LookCommand();

    ...

That means that in order to construct a Command, you need to construct a new LookCommand. But a LookCommand is a Command, so constructing one LookCommand requires constructing another one, which constructs another one, etc.

We don't know what you're trying to achieve with the l variable here, but that's what's causing the Either you need to get rid of that variable, or don't initialize it in that way, or make LookCommand not derive from Command.

people

See more on this question at Stackoverflow