ChemaPando
ChemaPando

Reputation: 11

DialogWindow cause a StackOverflow Exception

I have a dialog window with three buttons to select the Data Source in Application, also I have an Accept button and Cancel Button, but when I press any button the program cause a stack overflow exception.

public int SelDataSource
    {
        get { return SelDataSource; }
        set { SelDataSource = value; }
    }

    public formSelDataSource()
    {
        InitializeComponent();
        DialogResult = DialogResult.None;
    }

    private void rbtnSQLServer_CheckedChanged(object sender, EventArgs e)
    {
        SelDataSource = (int)DataSources.SQLServer;
    }

    private void rbtnAccess_CheckedChanged(object sender, EventArgs e)
    {
        SelDataSource = (int)DataSources.Access;
    }

    private void rbtnMySQL_CheckedChanged(object sender, EventArgs e)
    {
        SelDataSource = (int)DataSources.MySQL;
    }

    private void btnAceptar_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.None;
    }

    private void formSelDataSource_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (DialogResult == DialogResult.None)
            e.Cancel = true;
    }

    private void SelDataSourceButton(object sender, EventArgs e)
    {
        DialogResult = DialogResult.None;
    }

have anybody a solution?

Upvotes: 0

Views: 32

Answers (2)

zdimension
zdimension

Reputation: 1084

public int SelDataSource
{
    get { return SelDataSource; }
    set { SelDataSource = value; }
}

That is a self-reference. You are trying to make a property with backing field, but here the "backing field" is the property itself. When you access the property, it calls the get block, which accesses the property, which calls the get block, et caetera. You can either create a backing field, or replace it by an auto-property like this:

public int SelDataSource { get; set; }

Upvotes: 0

Eli Arbel
Eli Arbel

Reputation: 22749

This has nothing to do with the form. The property SelDataSource has an infinite recursion in it (the setter keeps calling itself, as does the getter).

You can use an auto-property instead:

public int SelDataSource { get; set; }

Upvotes: 1

Related Questions