Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need to get values from Form2 to Form1,
Form 2 is pop up screen in form1.

Currently what I am doing is,

1. Creating public Static Variable in Form2.
2. getting Value in Form1 like Form2.variablename.

is this best way?


we can do like this also ,
without creating static variable, by creating instance for the form class and accessing.

Whats is the best ways to do this?


Regards,
Pal
Posted

1 solution

No. Do not use static - it is unneccessary and dangerous, likewise, do not use a variable, use a property.
Try to avoid static properties at all times - they can cause nasty bugs if you do not realise what is happening.

Form2:
C#
public string UserName
   {
   get { return userNameTextBox.Text}
   set { userNameTextBox.Text = value; }
   }


Form1:
C#
Form2 f = new Form2();
f.UserName = "JoeCool";
if (f.ShowDialog() == DialogResult.OK)
   {
   Console.WriteLine(f.UserName);
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900