I am a newbie programmer who is now developing application on C# with Visual Studio 2010 and many forms, so i am interested is it possible to set some global variables to use them in all forms ? Simple example, i have 10 forms, all of them are connecting to MySQL database, so it is not really smart to set 10 times database name and password when in ideal i can set in once and then use everywhere, i can't find answer to this question in google, so maybe somebody can help me here? Is it possible?
Asked
Active
Viewed 1,211 times
0
-
6Global variables are evil. Don't use them. – Oded Mar 20 '13 at 21:40
-
3use settings http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx – VladL Mar 20 '13 at 21:41
-
1Related http://stackoverflow.com/q/484635/38206 – Brian Rasmussen Mar 20 '13 at 21:42
2 Answers
6
Create a base class for your form, something that inherits from Form
. Initialize the shared values there, to be accessible by the inheriting forms.

Oded
- 489,969
- 99
- 883
- 1,009
2
There are few ways you can solve this.
- First you can use settings.
- Second you could use web.config (not sure if this works in forms)
- Third option is to create a static class that hold your variables.

Icy Creature
- 1,875
- 2
- 28
- 53