views:

323

answers:

9

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#

+6  A: 

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.

Jon Skeet
But if they make it public static than anyone can use the variable! I can't think of anything more convenient.
ChaosPandion
What is wrong with public static variables?
James
Not wrong exactly but extremely easy to abuse.
ChaosPandion
@ChaosPandion what would be considered a better alternative then?
James
+1. In web applications global objects usually mean bad design, except of some cases in service layer, like Cache.
Vitaliy Liptchinsky
@James: It depends on the use case.
Jon Skeet
A: 

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.

Ian P
Why the downvote? It's the same comment as the one posted above me. lol
Ian P
A: 

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

Kevin
session is per user, not per application. So it's not truly global.
No Refunds No Returns
You can have both, I just didn't explicitly state application state.
Kevin
i m trying to use single object of a class that will process all my database handling.
Shantanu Gupta
A: 

A VB Module is basically a public static class in C#. But like Jon said, you really really have to come up with good reasons to use global variables.

Yann Schwartz
+3  A: 

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.

Canavar
Using the Cache object is preferable to using the Application object. The reason is that if memory utilization of the ASP.Net worker process approaches its limit, some Cache will be released to free up memory, but Application will not. Therefore, using Application objects can result in recycling the worker process, while using Cache will avoid that undesirable action.
DOK
A: 

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.

DOK
cached values can expire, leaving you with a NULL reference.
No Refunds No Returns
If you write your code correctly, you can check for NULL and recreate the cached value as needed. If the value never changes, it can be an appSetting in web.config.
DOK
A: 

Globals? just say no. Globals should be avoided at all costs. If it's configuration data, use the application configuration stuff.

Muad'Dib
A: 

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

A: 

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.

Chris
my connecton string is in web config only. there are some functions around 40-50 functions that are managing all database related task. I need to use these classes so that my work will become much easier.
Shantanu Gupta
Ahh I see ... i think you need to have a look at tiered architecture, put all your dbase classes in to a separate project, call it DAL or so and then in your main project import that DAL project namespace and reference all your classes from there. OR even cooler... look at SOA ...convert your classes into Web service and pass the data around using them (though make sure the objects you pass around do not have deep inheritance. (hope that makes sense?)
Chris