Question

I open an additional form through a Toolstrip to enter a Username that will be needed in the Mainform (and is declared as String in the Mainform)

Code of Mainform:

private void toolStripButton6_Click(object sender, EventArgs e)
    {
        using (Form frm = new Form3())
        {
            frm.FormBorderStyle = FormBorderStyle.FixedDialog;
            frm.StartPosition = FormStartPosition.CenterParent;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                Username = frm.ReturnValue1;
            }
        }
    }

Code of Form3:

    public string ReturnValue1 { 
        get
        {
            return textBox1.Text;
        }
    } 
    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

C# tells me that there is no frm.ReturnValue1 :(

Was it helpful?

Solution

You have declared your form as type Form not Form3:

using (Form frm = new Form3())

and as the class Form doesn't have a property ReturnValue1 you are getting the error. This compiles because Form3 is a subclass of Form so you can assign it to a variable of type Form without any casting being required. If you had it the other way round the compiler would have told you you needed a cast.

Your code should be:

using (Form3 frm = new Form3())

or perhaps even (my preference):

using (var frm = new Form3())

Then it will always be of the right type and you don't have to remember to change the class name in two places should you decide to use a different form in future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top