Stack Overflow Exception on Auto Properties

-2

my application is throwing Stack Overflow exceptions on the startup on some auto properties and objects creation.

They were working without problems, I have not modified them.

Basically I have an abstract class "Rule" that implements an interface "IRule", those properties are from the interface. Then I have a child class that inherits from Rule, I get the exception on that child class.

Edit:

public class RuleA: Rule
{
    private RuleA_bestSettings;

    #region PROPERTIES

    public override Rule BestSettings { get { return _bestSettings; } set { _bestSettings = value as RuleA; } }


    #endregion

    public RuleA()
    {
        Initialize();
    }

    protected override void Initialize()
    {
        base.Initialize();

        _bestSettings = new RuleA();
    }



}

}

Now I'm getting the exception on the Initialize method when I instantiate the property

This happens when a new object with those properties is instanciated.

Si รจ verificata l'eccezione System.StackOverflowException
  HResult=0x800703E9
  Messaggio=Generata eccezione di tipo 'System.StackOverflowException'.

I can't figure it out, any ideas? Thanks!

c#
exception
properties
stack
overflow
asked on Stack Overflow Sep 11, 2017 by Fabrizio402 • edited Sep 11, 2017 by Fabrizio402

1 Answer

0

In your Initialize section, you are instantiating a new RuleA. This new RuleA will construct itself and in so doing it will call its own Initialize section and create a third RuleA. Which will create a fourth. And a fifth. And so on, until the stack fills up.

I am not sure what you are trying to accomplish, but my guess is that instead of this

protected override void Initialize()
{
    base.Initialize();

    _bestSettings = new RuleA();
}

you meant to do this:

protected override void Initialize()
{
    base.Initialize();

    _bestSettings = this; 
}
answered on Stack Overflow Sep 11, 2017 by John Wu

User contributions licensed under CC BY-SA 3.0