2

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#

3
  • 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 objects Commented Nov 25, 2009 at 14:53
  • @Shantanu: What state do those objects have? Are they effectively just singletons? Commented 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? Commented Nov 25, 2009 at 15:45

9 Answers 9

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But if they make it public static than anyone can use the variable! I can't think of anything more convenient.
@ChaosPandion what would be considered a better alternative then?
+1. In web applications global objects usually mean bad design, except of some cases in service layer, like Cache.
@James: It depends on the use case.
3

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

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.
0

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

Why the downvote? It's the same comment as the one posted above me. lol
0

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.

Comments

0

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

cached values can expire, leaving you with a NULL reference.
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.
0

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

Comments

0

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

0

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

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.
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?)
0

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

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.