Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global variables in c#.net

How can I set a global variable in a C# web application?

What I want to do is to set a variable on a page (master page maybe) and access this variable from any page.

I want to use neither cache nor sessions.

I think that I have to use global.asax. Any help?

like image 548
scatman Avatar asked Mar 15 '10 06:03

scatman


People also ask

What is global and local variable in C?

A global variable exists in the program for the entire time the program is executed. A local variable is created when the function is executed, and once the execution is finished, the variable is destroyed. It can be accessed throughout the program by all the functions present in the program.

What is global variable explain with example?

It has a global scope means it holds its value throughout the lifetime of the program. Hence, it can be accessed throughout the program by any function defined within the program, unless it is shadowed. Example: int a =4; int b=5; public int add(){ return a+b; } Here, 'a' and 'b' are global variables.

Does C have global variables?

The C compiler recognizes a variable as global, as opposed to local, because its declaration is located outside the scope of any of the functions making up the program. Of course, a global variable can only be used in an executable statement after it has been declared.


1 Answers

Use a public static class and access it from anywhere.

public static class MyGlobals {     public const string Prefix = "ID_"; // cannot change     public static int Total = 5; // can change because not const } 

used like so, from master page or anywhere:

string strStuff = MyGlobals.Prefix + "something"; textBox1.Text = "total of " + MyGlobals.Total.ToString(); 

You don't need to make an instance of the class; in fact you can't because it's static. new Just use it directly. All members inside a static class must also be static. The string Prefix isn't marked static because const is implicitly static by nature.

The static class can be anywhere in your project. It doesn't have to be part of Global.asax or any particular page because it's "global" (or at least as close as we can get to that concept in object-oriented terms.)

You can make as many static classes as you like and name them whatever you want.


Sometimes programmers like to group their constants by using nested static classes. For example,

public static class Globals {     public static class DbProcedures {         public const string Sp_Get_Addresses = "dbo.[Get_Addresses]";         public const string Sp_Get_Names = "dbo.[Get_First_Names]";     }     public static class Commands {         public const string Go = "go";         public const string SubmitPage = "submit_now";     } } 

and access them like so:

MyDbCommand proc = new MyDbCommand( Globals.DbProcedures.Sp_Get_Addresses ); proc.Execute(); //or string strCommand = Globals.Commands.Go; 
like image 81
John K Avatar answered Sep 19 '22 12:09

John K