How to create global variables in C#?
-
My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)
-
My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)
-
My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)
Try something like this:
namespace MyGlobals { sealed public class DaysOfTheWeek { public static int nSunday = 0; public static int nMonday = 1; public static int nTuesday = 2; public static int nWednesday = 3; public static int nThursday = 4; public static int nFriday = 5; public static int nSaturday = 6; } }
You can reference these variables by name (nMonday, nFriday, etc.) in your other files after adding the following line:using MyGlobals.DaysOfTheWeek;
Click here to see my articles and software tools -
My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)
You could create an abstract super class that defines the variables that need to be global, then derive all of your classes from that one class. Alternatively, If you don't need to update the value of the variable, then you could define it in a .config file, and then read the config file from the classes that need to access the variable. If you need to change it or share it between application types (web, windows, windows services, etc.), then define an XML document somewhere on your system and then read from and update it in the classes where you use it. I'm sure there is probably a better way to do it, but this is my 2c. RabidK
-
My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)
Vitaliy Vorontsov wrote: My application requires global variables BTW the politically correct name for a global variable is "Well Known Object" ;P
-
Vitaliy Vorontsov wrote: My application requires global variables BTW the politically correct name for a global variable is "Well Known Object" ;P
-
My application requires global variables in it(the variable that can be accessed from objects of any class in application). How to implement such a feature ? :) Thanks in advance :)
try http://www.dofactory.com/patterns/PatternSingleton.aspx It helps with global variables.