.net - C# new Form return Value not recognised by Mainform -


i open additional form through toolstrip enter username needed in mainform (and declared string in 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 {                  {             return textbox1.text;         }     }      private void button1_click(object sender, eventargs e)     {         this.close();     } 

c# tells me there no frm.returnvalue1 :(

you have declared form type form not form3:

using (form frm = new form3()) 

and class form doesn't have property returnvalue1 getting error. compiles because form3 subclass of form can assign variable of type form without casting being required. if had other way round compiler have told you needed cast.

your code should be:

using (form3 frm = new form3()) 

or perhaps (my preference):

using (var frm = new form3()) 

then of right type , don't have remember change class name in 2 places should decide use different form in future.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -