Click here to Skip to main content
15,924,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a variable in form1 I want to pass it to form2 and other forms using c# how can i do this??


Thank you

[edit]Just spelling and capitalization - OriginalGriff[/edit]
Posted
Updated 25-Aug-10 9:04am
v2

Loads of ways:
Good:
1) Public Property in Form2; set it from Form1
2) Event with argument in Form1; subscribe to event in Form2

Bad:
1) Public field in Form2; set it in Form1
2) Public field in Form1; grab it in Form2
3) Static field/property in either Form1 or Form2, set/grab in other.

There are others, but that covers most ways.
 
Share this answer
 
Create a static global class that contains a static string variable, and set it/get it from anywhere in the application. Of course, global static vars are generally frown upon by OOZ (object oriented zealots), but hey, it gets the job done, and it's efficient.
 
Share this answer
 
Create a Static object (single instance if you want) outside any form and later call it using that Static Variable.

This will ensure that you will access the same form object from anywhere.

In case of VB.NET, you might use Form1(name of the class) to implicitly access the Application object.
 
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