daniyalahmad
daniyalahmad

Reputation: 3843

Cannot create object of inherit class of MainWindow

I am trying to create object of login which is inherit from MainWindow(Default window created in c# code) Here is my code :

  public partial class MainWindow : Window
    {
       login ins = new login();
       .
       .
       .
       .
    }
  public class login
    {
       public login(){}
    }

Its giving error : An unhandled exception of type 'System.StackOverflowException' occurred

Upvotes: 0

Views: 92

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61339

By deriving from MainWindow you invoke the instructor of MainWindow each time you instantiate login.

Thus, your code does:

  1. Make "MainWindow"
  2. "MainWindow" creates login
  3. "Login" inherits "MainWindow" so it makes a new one
  4. New "MainWindow" makes a new "Login"
  5. Reapeat ad infinitum
  6. StackOverflow!

Your "Login" class shouldn't derive from "MainWindow" (and neither should anything else!)

Upvotes: 1

Related Questions