Global variables.` These are useful. But they can lead to problems that are hard to debug. Global variables can cause errors in software development.`Many programs` can benefit from global variables if used in a controlled manner. We use the static keyword. A separate static class can also be used.`An example.` The static keyword describes something that is part of a type, not an instance of the type. This word was chosen for historical reasons—it was used in C and C++. `Next: `We see 2 files: one that contains the global variables in a static class, and Program.cs, which uses the global class.`Notes, above program.` GlobalString is an example of a public global variable. This field is constant. You must assign its value inline with its declaration at the class declaration space.`Notes, continued.` GlobalValue is a public static property with get and set accessors. This is an access routine of a backing store. The backing store is a field of type int. `The _globalValue field is modified through the set accessor. It is accessed through the get accessor.`Property `property`Notes, global field.` The global variable class contains a global field of Boolean type. The C# bool type aliases the System.Boolean type, which inherits from System.ValueType. `This bool field could cause problems in your program because it does not use an access routine.`Access routines.` Global variables are used most effectively when accessed through special methods called access routines. You can use access routines in nearly any programming language. `These methods provide another layer of abstraction over how you use the global variables.`In the example, the property accessor (get) is an accessor routine. Please see Code Complete by Steve McConnell, page 340.`Threads.` Static fields can introduce problems when using multithreading, which is used extensively in websites. An access routine could provide locking using the lock-statement. `Global Variables, ASP.NET `global-variables-aspnet`Lock `lock`This is a problem in many Windows Forms applications that use BackgroundWorker threads.`BackgroundWorker `backgroundworker`A summary.` We used global variables by adding a static class with constants, properties and public fields. You can also define methods with parameters to access the static variables.`We looked` an example of using these global variable types. We discussed issues relating to global variables and threading. We emphasized the usage of access routines. `Static `static

567Global variables class, GlobalVar.cs: C#7 7/6{<6B> /6{C69 global variables 6oproject. /6{</6B> 76=6A6DGlobalVar {7 5/6{<6B> 5/6{Global variable that is constant. 5/6{</6B> 576=const 6L 7Global6M76z7"Important Text"7;7 5/6{<6B> 5/6{Static 6d protected by access routine. 5/6{</6B> 576A6s_global6e;7 5/6{<6B> 5/6{Access routine 6oglobal variable. 5/6{</6B> 576=6A6s7Global6e7 5{ 55get 55{6'6J _global6e; 55} 55set 55{6'_global6e6z6d; 55} 5}7 5/6{<6B> 5/6{Global 6Afield. 5/6{</6B> 576=6Abool 7GlobalBoolean7; } 7 6P6O6S6D6; { 56$5{7 556{6h global constant 6L. 5576%7GlobalVar.Global6M7);7 556{Set global 6}eger. 557GlobalVar.Global6e76z400;7 556{Set global boolean. 557GlobalVar.GlobalBoolean76ztrue;7 556{6h the 2 previous 6ds. 5576%7GlobalVar.Global6e7); 556%7GlobalVar.GlobalBoolean7); 5} } 7 Important Text 400 True7

lglobal variables, Program.cs