I was working on window appication previously where i used to create global variables using modules in vb.net, but i found that in C# there is no concept of modules. so how can i create global vaiables in C#
-
basically i have some classes like data layer, buisness layer, that i used in windows applications. Now i want to use those classes. In vb.net i created public objects of those classes and used in whole application. But i dont know here how to create global objects. Is it good to use session for all those classes objectsShantanu Gupta– Shantanu Gupta2009-11-25 14:53:40 +00:00Commented Nov 25, 2009 at 14:53
-
@Shantanu: What state do those objects have? Are they effectively just singletons?Jon Skeet– Jon Skeet2009-11-25 15:40:41 +00:00Commented Nov 25, 2009 at 15:40
-
I think is might be helpful to consider this as two separate issues: 1) whether or not to use global variables for a particular purpose, and 2) if you do use them, what is the most appropriate way to do it?DOK– DOK2009-11-25 15:45:52 +00:00Commented Nov 25, 2009 at 15:45
9 Answers
Well, you can use public static variables in public classes... but I'd really urge you not to.
Do these variables values change? What do they represent? Don't forget that all users will use the same set of variables.
4 Comments
You're talking about a web application, so I will recommend you to use Session State if your variables will be used in one session only. And I will recommend you to use Application State for application wide variables.
1 Comment
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 at the beginning and try to learn the basics of OO programming.
Good luck.
1 Comment
In ASP.Net, you can create the equivalent of global variables using the Cache object.
Cache["someName"] = "some value";
One benefit of using Cache is that you can put objects in it.
Another benefit is that you can alter the value in 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.
2 Comments
Or use either application, session or viewstate objects depending on your needs. But as "Jon Skeet" claims, using global variables should be avoided. But if you insist take a look at this link: Microsoft KB Article
Comments
you COULD use the global.aspx file, and have properties set in there. but as mentioned above... Why do you need a global variable? If you are trying to hold connection strings and system wide static vars.. put it in the web.config.
sorry mis read.. thats for Web apps.
2 Comments
If you are using ASP.NET, I would look at using Session and Application state, since ASP.NET is stateless. This is assuming you are talking about storing "Global" information across requests and not just having a container to hold values which multiple objects can access during one requests. If my assumption is wrong, I would look at Jon's answer.
http://msdn.microsoft.com/en-us/library/ms972429.aspx
Based on the comments below, you are going to want to take a look at creating a singleton pattern.
http://www.dofactory.com/patterns/PatternSingleton.aspx
Before doing this though, I would really take a look at why you want to use it in a ASP.NET application as it can seriously hurt performance if implemented incorrectly.
Since what you want to do surrounds db stuff I would also take a look at:
http://www.15seconds.com/Issue/040830.htm and http://msdn.microsoft.com/en-us/magazine/cc163854.aspx