简体   繁体   中英

Proper way of using C# global variables in ASP.NET?

I am using ASP.NET Web Forms and C# in my application. I have a class file named Global.cs , in which I define variables using set and get properties. I use those variables anywhere on any pages by instantiating that class object.

Here is my Global.cs file:

using System;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// Contains my site's global variables.
/// </summary>
public static class Global
{
    /// <summary>
    /// Global variable storing important stuff.
    /// </summary>
    public static string gDate;
    public static string gMobLength;
    public static string gDateFormat;
    public static string gApplicationNo;
    public static string gBranchNo;
    public static string gMemId;
    public static string gIsEditable="false";
    public static string gLoggedInUserName;


    public static string ImportantData
    {
        get
        {
            return gDate;

        }
        set
        {
            gDate = value;

        }

    }
    public static string MobileLength
    {
        get
        {
            return gMobLength;
        }
        set
        {
            gMobLength = value;
        }
    }

    public static string DateFormat
    {
        get
        {
            return gDateFormat; 
        }
        set
        {
            gDateFormat = value; 
        }
    }
    public static string ApplicationNo
    {
        get
        {
            return gApplicationNo;
        }
        set
        {
            gApplicationNo = value; 
        }
    }
    public static string BranchNo
    {
        get
        {
            return gBranchNo; 
        }
        set
        {
            gBranchNo = value;
        }
    }

}

Is this a proper way of using variables throughout the project? What are the possible pros and cons with this approach and what approach would you guys take for using global variables?

First, I'd recommend using autoimplemented properties.

public static string BranchNo { get; set; }

Simplifies your code a bit. As to whether or not this is a good approach, it depends. Sometimes simple and straight-forward is better, and this falls into that category. If the values should not change once initialized, you may want to use a proper singleton with initialization:

public class Settings
{
   private static Settings _current;
   private static readonly object _lock = new object();

   public static Settings Current
   {
      get
      {
         lock(_lock)
         {
            if (_current == null) throw new InvalidOperationException("Settings uninitialized");
            return _current;
         }
      }
      set
      {
          if (value == null) throw new ArgumentNullException();
          if (_current != null) throw new InvalidOperationException("Current settings can only be set once.");

          if (_current == null)
          {
              lock(_lock)
              {
                 if (_current == null) _current = value;
              }
          }
      }
   }


   public string ImportantData { get; private set; }

   // etc. 
}

Initializing settings:

Settings.Current = new Settings{ ImportantData = "blah blah blah"};

Accessing:

var data = Settings.Current.ImportantData;

Outside of the two bromides "globals are bad" and " properties are good" ... there's nothing intrinsically wrong with your approach. Go for it!

IMHO .. PSM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM