Januszoff
Januszoff

Reputation: 315

Stack Overflow when trying to access form controls from class

I have a problem, after adding this code so I can access my MainWindow controls in Downloader class:

public partial class MainWindow : Form
{
    private Downloader fileDownloader;

    public MainWindow()
    {
        InitializeComponent();
        fileDownloader = new Downloader(this);
    }
//smth
}

and

class Downloader : MainWindow
{
    private MainWindow _controlsRef;

    public Downloader(MainWindow _controlsRef)
    {
        this._controlsRef = _controlsRef;
    }
// smth
}

it now gives me "An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll" on line

this.mainControlPanel.ResumeLayout(false);

in MainWindow.Designer.cs. If i comment out the code above, it works fine. Any ideas please?

PS. Also, when I'm in Downloader class, should i access the controls like

textbox.Text

or

_controlsRef.textbox.Text

Both seem to give no compile errors, is there any difference between the two?

Upvotes: 5

Views: 64

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176896

It's problem because here you are having cyclic dependancy i.e. both the method waiting for each other to complete.

check

public MainWindow()
    {
        InitializeComponent();
        fileDownloader = new Downloader(this);//this wait to complete downloader intialization
    }

  public Downloader(MainWindow _controlsRef)//this wait to complete mainwindow first
    {
        this._controlsRef = _controlsRef;
    }

my mean to say as you are inheriting Downloader from MainWindow , which in constructor crating downloader instace...and downloader calls Mainwindow constructor first as its parent crates problem for you here Solution

if want reference you can do like this

public partial class MainWindow : Form
{
    protected MainWindow mainWindow;

    public MainWindow()
    {
        InitializeComponent();
        mainWindow = this;
    }
//smth
}

class Downloader : MainWindow
{

    public Downloader()
    {
        //this.mainWindow //will give you reference to main winsow
    }
// smth
}

Upvotes: 1

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

Your Downloader class inherits MainWindow. When you instansiate it, according to the C# specification, the base class is initialized first. When MainWindow initializes, it creates a new instance of Downloader, which causes it eventually to stackoverflow, because you're in an endless cyclic dependency.

Since Downloader inherits MainWindow, there is no point in getting an instance of it via your constructor. You can simply access it's protected and public members from your derived.

For example:

public partial class MainWindow : Form
{
    protected string Bar { get; set; }
    public MainWindow()
    {
        Bar = "bar";
        InitializeComponent();
    }
}

public class Downloader : MainWindow
{
    public void Foo()
    { 
       // Access bar:
       Console.WriteLine(Bar);
    }
}

Upvotes: 3

Related Questions