Click here to Skip to main content
12,631,022 members (30,959 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Windows
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 29-Jul-11 22:17pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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:
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:
// 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 !
  Permalink  
v3
Comments
Sandeep Mewara 10-Jun-12 11:48am
   
Good answer. 5!
Satish Kumar Trivedi 8-Oct-12 10:07am
   
Nice Example!!!
Thanks
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try this link

Global Variable in C#[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

you can create a class in which you declare your global variables, like this:

public class A
{
  public static int myintglobal;
}

and you can access it like this:

A.myintglobal;


hope it helps :)

for further queries comment here!!
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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:

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:
// Read access
int value = MyNamespace.MyClass.MyProperty;
 
// Write access
MyNamespace.MyClass.MyProperty = 29;
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Declare it below your class for example

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


Here int i is declared globally
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 10 Jun 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100