Priyank Patel
Priyank Patel

Reputation: 7006

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?

Upvotes: 4

Views: 36718

Answers (3)

moribvndvs
moribvndvs

Reputation: 42495

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;

Upvotes: 4

Nas
Nas

Reputation: 1131

The reason why you do not see the variable after you instantiate that class object is because the variables are declared as static. Static variables are meant to be used by that manor ClassName.variableName or ClassName.PropertyName

Upvotes: 1

paulsm4
paulsm4

Reputation: 121881

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

Upvotes: 2

Related Questions