C# GLOBALS

Global values are useful to store constant values of your program or transfer values between different scopes. They can be accessed anywhere. Unfortunately there is not any predefined struct in .NET . Globals is defined using static class.

  1. using System;
  2.  
  3. //Declare GLOBALS class in global namespace or wherever you want
  4. public static class GLOBALS
  5. {
  6.     public const int KEY = 5000;
  7.     public static string STATUS = "NULL";
  8. }
  9.  
  10. namespace Test
  11. {
  12.     class Program
  13.     {
  14.         public static void MyMethod()
  15.         {
  16.            //Do something
  17.  
  18.             GLOBALS.STATUS = "MYMETHOD";
  19.         }
  20.  
  21.         static void Main(string[] args)
  22.         {
  23.             Console.WriteLine("KEY:{0,10:d}", GLOBALS.KEY);
  24.             Console.WriteLine("STATUS:   {0}", GLOBALS.STATUS);
  25.             Console.WriteLine();
  26.  
  27.             MyMethod();
  28.  
  29.             Console.WriteLine("KEY:{0,10:d}", GLOBALS.KEY);
  30.             Console.WriteLine("STATUS:   {0}", GLOBALS.STATUS);
  31.         }        
  32.     }
  33. }
m ant / Mechanical Ant