Zaryab Waseem Zaryab Waseem - 17 days ago 10
C# Question

An unhandled exception of type 'System.StackOverflowException' occurred in Slate.exe

I'm working on an application in C# and unexpectedly I started getting this exception "An unhandled exception of type 'System.StackOverflowException' occurred in Slate.exe".

Following are the details of my code.

This is the class in which I have initialized the object of my main class.

public class MenuControls
{
MainWindow MainWindowObj = new MainWindow(); //This thing is raising Exception.

public void SaveAs()
{

SaveFileDialog SFD = new SaveFileDialog();
if (SFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{

String FPath = SFD.FileName;
StreamWriter SWriter = new StreamWriter(File.Create(FPath));
SWriter.Write(MainWindowObj.PlayGround.Text);
SWriter.Dispose();

}
}

public void NewFile()
{
MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
}

public void OpenFile()
{
OpenFileDialog OFD = new OpenFileDialog();

if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamReader SReader = new StreamReader(File.OpenRead(OFD.FileName));
MainWindowObj.PlayGround.Text = SReader.ReadToEnd();
}
}
}


My main class code. where I have initialized the Object of my MenuConrols class.

public partial class MainWindow : Form
{
public RichTextBox GetBox()
{
return PlayGround;
}
MenuControls Menu = new MenuControls();
TextEditor_Preferences Prefrences = new TextEditor_Preferences();

public int SaveStatus = 0;
TextEditor_Preferences PreferencesForm = new TextEditor_Preferences();
public MainWindow()
{

InitializeComponent();

}

private void MainMenuFile_SaveAs_Click(object sender, EventArgs e)
{
Menu.SaveAs();
SaveStatus = 1;
}

private void MainMenuFile_New_Click(object sender, EventArgs e)
{
if (SaveStatus == 0 && PlayGround.Text.Contains(" "))
{
Menu.NewFile();
} else
{
PlayGround.Text = "";
}

}

private void MainMenuFile_Open_Click(object sender, EventArgs e)
{
Menu.OpenFile();
}

private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "N")
{
if (SaveStatus == 0 && !String.IsNullOrWhiteSpace(PlayGround.Text))
{
DialogResult DResult = MessageBox.Show("You haven't saved your file yet ! Do you want to create new file?", "Something", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (DResult == DialogResult.Yes)
{
PlayGround.Text = "";
} else
{

}
}
else
{
PlayGround.Text = "";
}
}
else if (e.Control && e.KeyCode.ToString() == "O")
{
Menu.OpenFile();
}
else if (e.Control && e.KeyCode.ToString() == "S")
{
Menu.SaveAs();
SaveStatus = 1;
}
}
private void MainMenuEdit_Preferences_Click(object sender, EventArgs e)
{
PreferencesForm.ShowDialog();
}

}


Please suggest me a solution. If I'm missing something or the question is not asked properly, please let me know explicitly.
Thanks.

Answer

This is because, you have a never ending code getting executed, you are instantiating an instance of MainWindow in MenuControls class:

public class MenuControls
{
    MainWindow MainWindowObj = new MainWindow(); // note this

and in MainWindow class, you have another field MenuControls which is getting instatiated, so when you create MainWindow instance, it instatntiates the MenuControls field, which again goes to insitialze the MainWindow field it has, and it keeps on doing that, hence resulting in stackoverflow exception.

public partial class MainWindow : Form
{
    public RichTextBox GetBox()
    {
        return PlayGround;
    }
    MenuControls Menu = new MenuControls(); // note this

You need to remove either MainWindow field from MenuControls class or vice versa, What i see is that you don't need MainWindow field in the MenuControl class, remove it from there.

Comments