Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 'System.StackOverflowException' in InitializeComponent()

Here are the main parts of the program where the exception seems to be concerned.

using System.Drawing;
using Framework.pages;

namespace Framework
{
   public partial class MainWindow : Window
   {
       public string status;
       public MainWindow()
       {
           InitializeComponent(); // Unhandled exception here
           InitializeTheme();

           activationStep page = new activationStep();
           page.loadPage();
       }
       // etc etc

This is the abstract class

 namespace Framework.pages
 {
     abstract class template : MainWindow
     { 
         public abstract void loadPage();
         public abstract void loadTheme();
     }
 }

This is the activation step class

using System.Windows.Media;

namespace Framework.pages
{
    class activationStep : template
    {
        public override void loadPage()
        {
            //this.loadTheme();
        }

        public override void loadTheme()
        {
            // Default green activation button
            //activateButton.Background = (SolidColorBrush)new BrushConverter().ConvertFromString(Framework.theme.darkGreen);
            //activateButton.BorderBrush = (SolidColorBrush)new BrushConverter().ConvertFromString(Framework.theme.borderGreen);
            // Set form error color to red
            //activationFormError.Foreground = System.Windows.Media.Brushes.Red;
        }
        // etc etc

The thing is if I comment out these two lines from the MainWindow class:

activationStep page = new activationStep();
page.loadPage();

The program runs fine despite the fact that everything within the activationStep class is commented out anyway (even if they aren't commented out too)? I'm just completely clueless as to why I'm getting this particular exception as there definitely doesn't seem to be any intense loops or anything.

-It's worth noting that there really are not many components loaded into the form and it runs smoothly usually.

like image 789
iyop45 Avatar asked Mar 22 '23 03:03

iyop45


1 Answers

You're "newing" up an activationStep, which derives from template, which in turn derives from MainWindow, whose constructor creates a new activationStep... etc, etc.

This loop runs for awhile, and then you get a StackOverflowException.

You'll need to rethink your design.

like image 140
Grant Winney Avatar answered Apr 25 '23 21:04

Grant Winney