Michael
Michael

Reputation: 39

Ref a current Form to the constructor of another Form

Hello i have newbie question. so i have to forms (main window & settings) and i want to pass the main form as a reference to the settings form so i can change variables from there.

i have this constructor in the settings:

    public Settings(ref Form1 form1)
    {
        this.form1 = form1;
    }

and this is my passing method:

    private void TsmiSettings_Click(object sender, EventArgs e)
    {
        Settings wSettings = new Settings(ref this );
    }

but "this" wont work cus its readonly..

any ideas how to solve this? or is there another better way to make things work?

Upvotes: 0

Views: 161

Answers (1)

Administrator
Administrator

Reputation: 327

You don't need to use the "ref" keyword.
Non-primitive variables are passed by reference and not by value.
Look at: Non-Primitive Types

All you have to do is pass "this" alone like this:

Settings wSettings = new Settings(this);

And on the Settings side the constructor should look something like this:

public class Settings
{
    private Form1 _form;

    public Settings(Form1 form)
    {
        this._form = form;
    }
}

Upvotes: 1

Related Questions