Creating global variables in asp.net using c #

I worked on a window application before when I used to create global variables using modules in vb.net, but I found that in C # there is no concept of modules. since i can create global vaiables in c #

+2
source share
9 answers

You are talking about a web application, so I recommend that you use Session State if your variables will be used in one session only. And I recommend that you use Application State for wide application variables.

+3
source

Well, you can use public static variables in public classes ... but I really urge you not to.

Do these variable values ​​change? What do they represent? Do not forget that all users will use the same set of variables.

+6
source

Static members of static classes?

A quick Google search for C # tutorials produced this result:

http://www.csharp-station.com/tutorial.aspx

You should start from the beginning and try to learn the basics of OO programming.

Good luck.

0
source

If you are using ASP.NET, I would look at using the Session and Application state since ASP.NET has no status. This assumes that you are talking about storing “global” information for all requests, and not just about having a container for storing values ​​that can be accessed by several objects during a single request. If my assumption is wrong, I would look at John's answer.

http://msdn.microsoft.com/en-us/library/ms972429.aspx

Based on the comments below, you'll want to take a look at creating a singleton template.

http://www.dofactory.com/patterns/PatternSingleton.aspx

Before doing this, I would really look at why you want to use it in an ASP.NET application, as this can seriously hurt performance if it is not implemented correctly.

Since what you want to do surrounds the db stuff, I will also look:

http://www.15seconds.com/Issue/040830.htm as well as http://msdn.microsoft.com/en-us/magazine/cc163854.aspx

0
source

The VB module is basically an open static class in C #. But, as John said, you really need to come up with good reasons to use global variables.

0
source

In ASP.Net, you can create the equivalent of global variables using the Cache object.

Cache["someName"] = "some value"; 

One of the benefits of using a cache is that you can put objects into it.

Another advantage is that you can change the value in the code.

You can also use the Application object, but Cache is preferred because of memory management.

As always, you should avoid overuse of these global variables and minimize their size.

0
source

Globals? just say no. Globals should be avoided at all costs. If this is configuration data, use the application configuration material.

0
source

Or use application, session, or viewstate objects depending on your needs. But, as John Skeet states, the use of global variables should be avoided. But if you insist, take a look at this link: Microsoft KB article

0
source

YOU CAN use the global.aspx file and have the properties set there. but, as mentioned above ... Why do you need a global variable? If you are trying to keep connection strings and static vars in the system. put it in the web.config file.

Sorry, incorrectly read .. thats for web applications.

0
source

All Articles