How to define Global Variables in C#.Net

This Article gives you idea about how to define Global Variables in and access these variables from anywhere in project. Unlike ASP.NET there is no Session or Application variables in C#.NET that will help us to store values globally.

Every project needs some data Transfer from a Page to another.
Depending upon the coding language and environment, Data transfer methods are vary.
Let's take an example of ASP.NET which having it's own State Management Techniques that are helpful for data transfer.

Following simple four questions will clear all your idea

Q1. Can i transfer data using C#.NET ?
Yes, we can transfer data by Creating constructor of forms and various methods are there. Here is my atricle

ASP.NET provide us a very easy way to define Global veriable Session variable that are unique to every login user. Session can be used for Global Variables.

Q2. Can i declare global variable in
Yes, we can define global variable in which are accessible everywhere in the project.

Q3. How can i declare global variable in
Follow the steps that allow you to declare global variable in

Step 1. Open up your form (on which you have to declare) global variable
Step 2. You will see the class declaration under namespace Just define the variable.


namespace namespace1
public class clsAccess
public static int iGlobal;

Q4. How can i use it anywhere in project
Access the veriable like ClassName.Veriable name

MessageBox.Show("Global Variables = " + clsAccess.iGlobal.ToString())

No need to create extra class object.

Hope it helps all.



Guest Author: mahajan10 Jun 2012

noce post, more or less the same as

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: