Click here to Skip to main content
15,912,072 members
Please Sign up or sign in to vote.
3.83/5 (5 votes)
See more:
New in windows application,

I want to define global veriable in c#.net that i can access anywhere in my project (Like Session in web application)
Posted

While you can "get away with" using variables with the 'static' modifier to make a "global variable" ... or define methods as 'static' to get a 'global method' ... I agree, strongly, with Philippe Mori that global variables are best expressed as properties in order to control their scope and use.

Why, you may ask, does one need to control scope and use ?

... Because use of global variables ... or methods ... is often a source of confusion in coding, and difficulty in development, code testing and maintenance, depending on the context of project development, the scenario of real-world use of the project ...

This brief example (.NET 4.0) demonstrates even "stricter" control of a global variable:
C#
namespace GlobalVariables
{
    public static class Globals
    {
        // parameterless constructor required for static class
        static Globals() { GlobalInt = 1234; } // default value

        // public get, and private set for strict access control
        public static int GlobalInt { get; private set; }

        // GlobalInt can be changed only via this method
        public static void SetGlobalInt(int newInt)
        {
            GlobalInt = newInt;
        }
    }
}
And, we'd use this example like so anywhere in the project:
C#
// activate the GlobalVariables NameSpace
using GlobalVariables;

// accessing the global variable
int x = Globals.GlobalInt;

// setting the global variable
Globals.SetGlobalInt(888);

By "forcing" yourself, or another developer, to activate a distinct NameSpace, and specify the Class to access the global variable, and by limiting the setting of the global variable to a specific procedure invoked in the static class, I'd argue that you are creating code that is self-documenting, and code in which global variable use is rigorously controlled.

In the above example, in the VS Studio 2010 Pro IDE, in a project in which the GlobalVariables NameSpace has been activated by a 'using' statement: an attempt to directly set the global variable of the form:
Globals.GlobalInt = 122;

Is going to give you a 'red flag' the moment you type it, and a useful error when you try to build the project. imho that's valuable !
 
Share this answer
 
v3
Comments
Sandeep Mewara 10-Jun-12 11:48am    
Good answer. 5!
Satish Kumar Trivedi 8-Oct-12 10:07am    
Nice Example!!!
Thanks
try this link

Global Variable in C#[^]
 
Share this answer
 
Under your class define a veriable and access it with class name, Here is example

namespace Product1.Forms
{
    public partial class myForm : Form
    {
       static int iCount = 10;
    }
}

//access veriable like
myForm.iCount

//No need to create form's object
 
Share this answer
 
you can create a class in which you declare your global variables, like this:

C#
public class A
{
  public static int myintglobal;
}


and you can access it like this:

C#
A.myintglobal;



hope it helps :)

for further queries comment here!!
 
Share this answer
 
v2
In fact, you should avoid global variables if possible.

Instead uses properties as it will allows you to modifier get and set function if the need arrise.

Thus an improved solution would be:

C#
namespace MyNamespace
{
    // A static class should be used for that purpose if the 
    // only intent of the class is to provide global objects.
    static class MyClass
    {
        // With recent C#, you can uses auto-implemented properties
        // until you have a need to customize the methods.
        public int MyProperty { get; set; }
    } 
}


And it would be used like that:
C#
// Read access
int value = MyNamespace.MyClass.MyProperty;

// Write access
MyNamespace.MyClass.MyProperty = 29;
 
Share this answer
 
Declare it below your class for example

C#
namespace Graphtry1
{
    public partial class Form1 : Form
    {
        int i;



Here int i is declared globally
 
Share this answer
 
Comments
Member 10636319 12-Feb-17 20:48pm    
Global stVariable as string (vb.net)

how to declare these in C#.net

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900