Global variables in c#.net

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?


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;

C# Global Variable Examples (Public Static Property), Use a global variable, implemented as public static property. Store globals in a static class. The global variable can be accessed from any function or class within the namespace. Does C# support Global Variables? C# is an object-oriented programming (OOP) language and does not support global variables directly. The solution is to add a static class containing the global variables. Using a global variable violates the OOP concept a bit, but can be very useful in certain circumstances.


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.

Global Variable In C#, can be accessed from any function or class within the namespace. Global variables are used most effectively when accessed through special methods called access routines. You can use access routines in nearly any programming language. Tip: These methods provide another layer of abstraction over how you use the global variables. And: In the example, the property accessor (get) is an accessor routine. Please see Code Complete by Steve McConnell, page 340.


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.

What are global and local variables?, . Now, you can access it from anywhere in your forms after assigning a value to it. In web applications, you can make a variable global by using the app state of the application. You can also declare variable in a session for the session level. But in a WinForms application, you can't use app or session states. However, you can make a global variable by creating a static class in a separate class file in your application.


You can create a variable with an application scope

Declare global variables in Visual Studio 2010 and VB.NET, How do you declare a global variable in VB net? Variables are always destroyed in reverse order of their creation, whether they are local or global / static variables (an exception is the primitive types, int,enums, etc., which are never destroyed if they are global / static until they end the program).


/// <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;
}

How to use Global Variables in C#?, could be accessible in all your forms in your project if you use the keyword public shared if it is in a class. Sometimes in C programming, a variable must be like cellular phone service: available everywhere. The variable also can be used by any function at any time. This type of variable could be called a universal variable. It could be called a worldwide variable. But in C, it’s referred to as a global variable. How to …


Global and Local Variables in C#, In C# you cannot define true global variables (in the sense that they don't Globals can be hard to debug with race conditions and many other  A local variable is a variable that is declared inside a function. A global variable is a variable that is declared outside all functions. A local variable can only be used in the function where it is declared. A global variable can be used in all functions. See the following example: #include<stdio.h> // Global variables int A; int B


[Solved] How to declare Global Variable..in C-sharp ?, C# Global Variable. Learn how to declare global variables in C#. Learn more about Session and Application States in ASP.NET. Global  How can I create global variables that are shared in C? If I put it in a header file, then the linker complains that the variables are already defined. Is the only way to declare the variable in one of my C files and to manually put in externs at the top of all the other C files that want to use it? That sounds not ideal.


C# Tutorial 28 - Global Variables, Local Variables A local variable is used where the scope of the variable is within the method in which it is declared. They can be used only by  Global Variables. C# do not support global variables directly and the scope resolution operator used in C++ for global variables is related to namespaces. It is called global namespace alias. If you have a type that share an identifier in different namespace, then to identify them use the scope resolution operator.