How to define Global Variables in C#.Net


This Article gives you idea about how to define Global Variables in C#.net 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 c#.net
Yes, we can define global variable in C#.net. which are accessible everywhere in the project.

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

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.

e.g.


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
e.g.

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




No need to create extra class object.

Hope it helps all.

Regards
koolprasad2003


Comments

Guest Author: mahajan10 Jun 2012

noce post, more or less the same as
http://webgeek.elletis.com/how-to-create-global-functions-in-asp-net-4-0/



  • 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:
    Email: