Scope of Shared Variables
-
Hi, just a little clarrification required please! I am in the process of developing several ASP.NET User Controls and a class library to plug into a third party CMS. What I have done with regards to the connection string and the fact that my class library cannot see the web.config file (and this may not be the best approach in which case I'm all ears!) is to have a shared class variable, called ConnectionString, which has a value assigned to it by my User Controls on each load of the User Control. But what I have found is that if I set the ConnectionString variable once, it seems to be available even after post back without having to set it again. What's more, even if I close the browser and load the page again, the shared connection string variable still seems to be there. Could someone please shed a little light on the scope of shared variables in this context and whether it's a good or bad idea to use them. Thanks :)
-
Hi, just a little clarrification required please! I am in the process of developing several ASP.NET User Controls and a class library to plug into a third party CMS. What I have done with regards to the connection string and the fact that my class library cannot see the web.config file (and this may not be the best approach in which case I'm all ears!) is to have a shared class variable, called ConnectionString, which has a value assigned to it by my User Controls on each load of the User Control. But what I have found is that if I set the ConnectionString variable once, it seems to be available even after post back without having to set it again. What's more, even if I close the browser and load the page again, the shared connection string variable still seems to be there. Could someone please shed a little light on the scope of shared variables in this context and whether it's a good or bad idea to use them. Thanks :)
shared (static in C#) has got a lifetime until the application domain unloads. Every .NET process will have an application domain associated with it and ASP.NET process will also have one. So your shared variables will end when ASP.NET service ends. shared variables are OK in your case (just for keeping the connection string).
Navaneeth How to use google | Ask smart questions
-
shared (static in C#) has got a lifetime until the application domain unloads. Every .NET process will have an application domain associated with it and ASP.NET process will also have one. So your shared variables will end when ASP.NET service ends. shared variables are OK in your case (just for keeping the connection string).
Navaneeth How to use google | Ask smart questions
OK great, thanks for the clarification :)
-
Hi, just a little clarrification required please! I am in the process of developing several ASP.NET User Controls and a class library to plug into a third party CMS. What I have done with regards to the connection string and the fact that my class library cannot see the web.config file (and this may not be the best approach in which case I'm all ears!) is to have a shared class variable, called ConnectionString, which has a value assigned to it by my User Controls on each load of the User Control. But what I have found is that if I set the ConnectionString variable once, it seems to be available even after post back without having to set it again. What's more, even if I close the browser and load the page again, the shared connection string variable still seems to be there. Could someone please shed a little light on the scope of shared variables in this context and whether it's a good or bad idea to use them. Thanks :)
Liqz wrote:
Could someone please shed a little light on the scope of shared variables in this context and whether it's a good or bad idea to use them.
This seems to be asked often lately. The scope of a static/shared variable in ASP.NET is the same as in their languages in general. I would not use them for anything.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Liqz wrote:
Could someone please shed a little light on the scope of shared variables in this context and whether it's a good or bad idea to use them.
This seems to be asked often lately. The scope of a static/shared variable in ASP.NET is the same as in their languages in general. I would not use them for anything.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Christian Graus wrote:
I would not use them for anything.
OK thanks, I appreciate the advice. The only other way I can think of to make the connection string available to my class library is to pass it into the constructor when instantiating objects. Is this the method you would use or do you have any other suggestions?