My code throws stackoverflowexception when i execute

1

I am working on a Windows Application Development in c# language. I have a template form => Masterpage which is inherited by all my other forms.

It has menustrip to open all other forms as well:

my error reads:

System.StackOverflowException
  HResult=0x800703E9
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

my Masterpage code is:

namespace WindowsProject
{
    public partial class Masterpage : Form
    {
        BookEditor bookE;
        StudentEditor studentE;
        ViewBorrowedBooks viewB;
        IssueBook issueBook;

        public Masterpage()
        {
            InitializeComponent();
            bookE = new BookEditor();
            studentE = new StudentEditor();
            viewB = new ViewBorrowedBooks();
            issueBook = new IssueBook();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void showForm(Form f)
        {
            bookE.Hide();
            studentE.Hide();
            viewB.Hide();
            issueBook.Hide();
            this.Hide();
            f.Show();
        }

        private void booksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showForm(bookE);
        }

        private void studentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showForm(studentE);
        }

        private void viewBorrowedBooksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showForm(viewB);
        }

        private void issueBookToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showForm(issueBook);
        }
    }
}

all my other forms inherits from Masterpage

c#
stack-overflow
asked on Stack Overflow May 21, 2020 by Xetree • edited May 21, 2020 by Iliar Turdushev

2 Answers

2

Why does StackOverflowException occur?

StackOverflowException occurs because of the infinite recursion inside the constructor of the Masterpage.

Lets consider how this recursion occurs. BookEditor inherits Masterpage. (The following conclusions are also applicable to the other inheritors of the Masterpage). I suspect that BookEditor has a single constructor without parameters BookEditor(). In C# if you call constructor without parameters of the derived type it by default calls constructor without parameters of the base type (if you don't explicitly specify other constructor of the base type). Lets assume that we want to create an instance of the Masterpage. We call its constructor Masterpage(). Inside it we call constructor of the BookEditor. But BookEditor inherits Masterpage and therefore it calls constructor of the Masterpage. Constructor of the Masterpage again calls constructor of the BookEditor and etc. These sequence of the constructor calls leads to the infinite recursion and StackOverflowException as a result.


How should you fix this problem?

I think that more information is needed to thoroughly answer this question.

As a workaround you should create inheritors of the Masterpage outside of its constructor:

public partial class Masterpage : Form
{
    private readonly IEnumerable<Form> _forms;

    public Masterpage()
    {
        InitializeComponent();
    }

    // Now we set forms here, not in the constructor.
    public void SetForms(IEnumerable<Form> forms)
    {
        _forms = forms;
    }

    private void showForm(Form f)
    {
        foreach (Form form in _forms)
            form.Hide();

        this.Hide();
        f.Show();
    }

    // Other members of the Masterpage.
    ...
}

// Palce this code where you create and show Masterpage.
var bookE = new BookEditor();
var studentE = new StudentEditor();
var viewB = new ViewBorrowedBooks();
var issueBook = new IssueBook();

var forms = new List<Form> {bookE, studentE, viewB, issueBook};
bookE.SetForms(forms);
studentE.SetForms(forms);
viewB.SetForms(forms);
issueBook.SetForms(forms);

var masterPage = new Masterpage();
masterPage.SetForms(forms);
answered on Stack Overflow May 21, 2020 by Iliar Turdushev • edited May 21, 2020 by Iliar Turdushev
0

Thank you all for answering my question. The problem i found on my code was it was looping infinitely since i put a child form inside the parent MDI form and instantiated the child form inside parent form’s constructor. The problem was solved by instantiating the forms outside the constructor inside a method.

answered on Stack Overflow May 29, 2020 by Xetree

User contributions licensed under CC BY-SA 3.0