1

I am getting a StackOverflowException when debugging my c# array declaration, which goes like this:

public class ConfigClass : MainWindow
{
    private const int totalIndex = 3;
    public int[][][] Config = new int[totalIndex][][]
    {
        new int[1][]
        {
            new int[] {10, 5, 5, 5, 1, 4},
        },
        new int[9][]
        {
            new int[] {4, 3, 1, 4, 2},
            new int[] {4, 8, 6, 7, 5},
            new int[] {2, 2, 4},
            new int[] {2, 2, 4},
            new int[] {0, 2, 2, 2, 2, 2, 2, 2, 2},
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0},
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0},
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0},
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0},
        },
        new int[9][]
        {
            new int[] {4, 1, 2, 3, 4},
            new int[] {4, 5, 6, 7, 8},
            new int[] {2, 2, 4},
            new int[] {2, 2, 4},
            new int[] {0, 3, 3, 3, 3, 3, 3, 3, 3},
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0},
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0},
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0},
            new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0},
        },
    };
}

public partial class MainWindow : Window
{
    ConfigClass Core = new ConfigClass();
    public DateTime systemDateTime = new DateTime();

    public static class ReferenceTo
    {
        // for group
        public const int group_A = 0;
        public const int group_B = 1;
        public const int group_C = 2;
        public const int group_D = 3;
    }
    public MainWindow()
    {
        InitializeComponent();
        StartSecondTimer();
    }

    public void UpdateTime()
    {
        label_systemTime.Content = systemDateTime.ToLongTimeString();
        label_systemDate.Content = systemDateTime.ToLongDateString();
    }

    public void StartSecondTimer() // start one second timebase
    {
        DispatcherTimer secondTimer = new DispatcherTimer();
        secondTimer.Tick += new EventHandler(dispatcherSecond_Tick);
        secondTimer.Interval = new TimeSpan(0, 0, 1);
        secondTimer.Start();
    }

    private void dispatcherSecond_Tick(object sender, EventArgs e) // one second timebase event handler
    {
        systemDateTime = DateTime.Now;
        UpdateTime();
    }
}

It is saying I have might have infinite loop or infinite recursion. What am I missing here?

Thanks! :)

EDIT: I added the 'public class ConfigClass : MainWindow' which is the source of the error.

8
  • 2
    This runs perfectly for me. The code you've posted is not the issue Commented Apr 11, 2016 at 3:03
  • If your declaration were the issue, the code would fail to compile, not throw a run time exception. Commented Apr 11, 2016 at 3:06
  • Hi guys, thanks for the prompt response. I have added the declaration that causes the problem, which is 'public class ConfigClass : MainWindow' Commented Apr 11, 2016 at 3:09
  • @ExtraFries That line would not throw a stack overflow exception. It's possible your code in MainWindow is broken. Can you please tell us and show us the exact line that throws the exception? That is - when your debugger breaks, show us the line it's stuck on Commented Apr 11, 2016 at 3:09
  • @Rob: it breaks just before the whole public int[][][] Config = new int[totalIndex][][] declaration, but here's where it stuck at MainWindow: ConfigClass core = new ConfigClass(); Commented Apr 11, 2016 at 3:16

1 Answer 1

2

Your infinite loop is because of this:

ConfigClass Core = new ConfigClass();

in your MainWindow class.

Every time you create a ConfigClass, the MainWindow constructor (and field initialization) will run as well. However, MainWindow's field initialization includes creating another ConfigClass. And so its constructor and super classes constructor will run.

It's a bit difficult to understand what you're trying to do (it's a bit suspect to have a reference to a sub class in your MainWindow) - but the above is the cause of your problems.

From your comment:

My purpose for inheriting MainWindow on ConfigClass is because I want to use the Reference.To class to look things up in arrays.

You don't need to inherit from MainWindow to do that. Any part of your code can write MainWindow.ReferenceTo without an issue

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Rob. Makes total sense now. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.