Declaring Global Variables in C# / Asp.net

Lets first consider what is exactly do u mean by Global and Local variables.

Local variables

Local variables must always be defined at the top of a block.

When a local variable is defined – it is not initalised by the system, you must initalise it yourself/user.

A local variable is defined inside a block and is only visable from within the block.

main()            {

int i=4;      i++;

}

A local variable is visible within nested blocks unless a variable with the same name is defined within the nested block.

main()

{

int i=4;  int j=10;

i++;

if (j > 0)

{

printf(“i is %d”,i); /* i defined in ‘main’ can be seen */

}

if (j > 0)

{

int i=100;   /* This ‘i’ local to this block */

printf(“i is %d”,i);

}           /* for ‘i’ (value 100) no scope here */

printf(“i is %d”,i); /* ‘i’ (value 5) is now visable. */

}

Global variables

Global variables are initalised by the system when you/user define them, for example in c when u declare a gloabal variable without value then depending upon its data types values are assigned to them by Data Type Initialser as follows

Datatype   Initial Value

int                    0

char                 ”

float                 0

pointer             NULL


In the next example i is a global variable, it can be seen and modified by main and any other functions that may reference it.

int i=4;

main()

{
i++;

}


Now, following example has global and Internal variables.

int i=1; /* Global definition */

main()

{

i++; /* Global variable */

func();

}

func()

{

int i=10; /* Local declaration */

i++; /* Local variable */

}

i in main is global and will be incremented to 2. i in func is internal and will be incremented to 11. When control returns to main the internal variable  will die and and any reference to i will be to the global.

we need seriously consider why we want a global variable, as sometimes more numberof global varibles means memory overhead.

In C# there is no provision for declaring Global Variable as such , but Instead we can declare global variable in the following ways

1) In the Global.asax as application variable,

The Appliacation variable will help in case you want a global variable accessed throughout the application by different sessions.

Application(“MyVar”) = “Check” or

Application[“Title”] = “Sample”;

2) In section of Web.config ,

You can do this by inserting your global variables in section of the web config file like this To access your variable in the code behind use this string

MyGlobalVariable = ConfigurationSettings.AppSettings[“MyGlobalVariable”];

Of course you can store what ever value type you want, i.e. it doesnt need to be string all the time. For example you can store boolean value ‘false’ but you need to cast it to a boolean when retreive it from web config.

Declaring global variable in web.config
Declaring global variable in web.config

3) As , Static variable of the class ,

Just use a static variable in a class and use that variable for in all the places in your application. It will work as a global variable.

static class GlobalClass

{

private static string m_globalVariable = “”;

public static string GlobalVariable

{

get {

return m_globalVariable;

}

set

{

m_globalVariable = value;

}

}

}

we can use it anywhere like this (bcoz it’s static we don’t instantiate it anywhere)

GlobalClass.GlobalVariable = “Some value”;

retrieve it anywhere like this:

myLabel.Text = GlobalClass.GlobalVariable;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s