Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8064225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:18:46+00:00 2026-06-05T11:18:46+00:00

I am using ASP.NET Web Forms and C# in my application. I have a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:18:49+00:00Added an answer on June 5, 2026 at 11:18 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET Web Forms application and I'm using some dynamic controls in
I have an ASP.NET web application that is using forms authentication. Everything is configured
My team and I have a asp.net web forms application and are using several
I have a jqGrid which I am using with asp.Net Web Forms , it
I am working on an ASP.NET web forms application that is using Forms Authentication.
I am building a Web Application using asp.net (C#). I come from windows forms
I have an ASP.NET 4 Web Forms app that is using URL Routing. I
I'm using the Entity Framework for an ASP.NET Web Forms application and I'm wondering
I have a pretty big web application that I created last year using ASP.NET
I have an ASP.NET MVC application which is using Linq to SQL classes placed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.