49

How can I set a global variable in a C# web application?

What I want to do is to set a variable on a page (master page maybe) and access this variable from any page.

I want to use neither cache nor sessions.

I think that I have to use global.asax. Any help?

5
  • Is there a particular reason cache/session wont work? Technically the Application is a Session/Cache object and so if you are using that (in a non-static way) you are already using Sessions. If its the session specific part, that can be addressed with the App level cache. Commented Mar 15, 2010 at 6:28
  • yes because i need to store a big list, and the cache is only 16MB so i don't want to waste cahe resources...
    – scatman
    Commented Mar 15, 2010 at 6:30
  • Do you want a singleton? A single object that is shared by all threads handling all requests? Or do you just want a way of share data between a MasterPage and a ContentPage. If it's the later then global variables are certainly not a good solution.
    – tarn
    Commented Mar 15, 2010 at 6:30
  • i need to share global variables between all pages in my project. not only between the master page and the content page.
    – scatman
    Commented Mar 15, 2010 at 6:33
  • 16MB? Do you have a reference for that? I have never heard of that limit before. I know I have used operational caches in the multi-gigabyte range on some servers. Commented Mar 15, 2010 at 6:33

6 Answers 6

117

Use a public static class and access it from anywhere.

public static class MyGlobals {
    public const string Prefix = "ID_"; // cannot change
    public static int Total = 5; // can change because not const
}

used like so, from master page or anywhere:

string strStuff = MyGlobals.Prefix + "something";
textBox1.Text = "total of " + MyGlobals.Total.ToString();

You don't need to make an instance of the class; in fact you can't because it's static. new Just use it directly. All members inside a static class must also be static. The string Prefix isn't marked static because const is implicitly static by nature.

The static class can be anywhere in your project. It doesn't have to be part of Global.asax or any particular page because it's "global" (or at least as close as we can get to that concept in object-oriented terms.)

You can make as many static classes as you like and name them whatever you want.


Sometimes programmers like to group their constants by using nested static classes. For example,

public static class Globals {
    public static class DbProcedures {
        public const string Sp_Get_Addresses = "dbo.[Get_Addresses]";
        public const string Sp_Get_Names = "dbo.[Get_First_Names]";
    }
    public static class Commands {
        public const string Go = "go";
        public const string SubmitPage = "submit_now";
    }
}

and access them like so:

MyDbCommand proc = new MyDbCommand( Globals.DbProcedures.Sp_Get_Addresses );
proc.Execute();
//or
string strCommand = Globals.Commands.Go;
0
7

I second jdk's answer: any public static member of any class of your application can be considered as a "global variable".

However, do note that this is an ASP.NET application, and as such, it's a multi-threaded context for your global variables. Therefore, you should use some locking mechanism when you update and/or read the data to/from these variables. Otherwise, you might get your data in a corrupted state.

2
  • is this a drawback only for global variables? or for the cache also?
    – scatman
    Commented Mar 15, 2010 at 6:44
  • AFAIK, the public methods of the Cache object that comes with an ASP.NET application are thread safe. But do take a look at the documentation...
    – Ron Klein
    Commented Mar 15, 2010 at 9:30
4

You can create a base class in your application that inherits from System.Web.UI.Page. Let all your pages inherit from the newly created base class. Add a property or a variable to your base class with propected access modifier, so that it will be accessed from all your pages in the application.

0

You can create a variable with an application scope

1
  • 1
    This link is dead.
    – Sam White
    Commented Aug 17, 2020 at 17:46
-1
/// <summary>
/// Contains global variables for project.
/// </summary>
public static class GlobalVar
{
/// <summary>
/// Global variable that is constant.
/// </summary>
public const string GlobalString = "Important Text";

/// <summary>
/// Static value protected by access routine.
/// </summary>
static int _globalValue;

/// <summary>
/// Access routine for global variable.
/// </summary>
public static int GlobalValue
{
get
{
    return _globalValue;
}
set
{
    _globalValue = value;
}
}

/// <summary>
/// Global static field.
/// </summary>
public static bool GlobalBoolean;
}
-4

Just declare the variable at the starting of a class.

e.g. for string variable:

public partial class Login : System.Web.UI.Page
{
    public string sError;

    protected void Page_Load(object sender, EventArgs e)
    {
         //Page Load Code
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.