Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Scope of Shared Variables

Scope of Shared Variables

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netwinforms
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Royall
    wrote on last edited by
    #1

    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 :)

    N C 2 Replies Last reply
    0
    • J Jay Royall

      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 :)

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • N N a v a n e e t h

        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

        J Offline
        J Offline
        Jay Royall
        wrote on last edited by
        #3

        OK great, thanks for the clarification :)

        1 Reply Last reply
        0
        • J Jay Royall

          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 :)

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • C Christian Graus

            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.

            J Offline
            J Offline
            Jay Royall
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups