4

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?

3 Answers 3

4

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;
Sign up to request clarification or add additional context in comments.

5 Comments

@HackedByChinese Thanks for the suggestion , What is the difference between my approach and the approach you suggested to simplify my code.Thanks.
Autoimplemented properties automatically generate the backing field, so you don't have to include them in your source.
Wouldn`t a simple Lazy be enough?
Settings.Current = new Settings{ ImportantData = "blah blah blah"}; will cause a compilation error because the "set" is unnaccesible.
Here is the fiddle which demonstrates my previous comment.
2

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

2 Comments

Ok thanks for the advice , can I ask what is IMHO PSM , might be sounding silly.Thanks.
"PSM" is my name :). "IMHO" means "In My Humble Opinion". And I honestly didn't notice that you were already using properties: I thought you just had a bunch of public static variables. Which, IMHO, can be perfectly OK.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.