Best place to set a global variable

Discussion in 'ASP .Net' started by Schoo, Feb 26, 2004.

  1. Schoo

    Schoo Guest

    I have some program constants that I need to set and I thought it would be
    best to set them all up in a central location so that I can change them
    quickly when we go from development to production. What is the best way to
    do that? Should I put them in web.config? Should I create a special class
    for these settings? Wherever it is best to write them, can you give me a
    line or 2 of code to show the best way to set a constant string?

    Thanks,

    Scott
    Schoo, Feb 26, 2004
    #1
    1. Advertising

  2. > I have some program constants that I need to set and I thought it would
    be
    > best to set them all up in a central location so that I can change them
    > quickly when we go from development to production. What is the best way

    to
    > do that? Should I put them in web.config? Should I create a special

    class
    > for these settings? Wherever it is best to write them, can you give me

    a
    > line or 2 of code to show the best way to set a constant string?


    You could put them in the Web.config's <appSettings> block, that's one
    option (more information at
    http://aspnet.4guysfromrolla.com/articles/053102-1.aspx). The
    disadvantage with this is if you have multiple Web applications that need
    to share constants you have to replicate the <appSettings> section in
    numerous projects.

    Another option (that is useful for sharing constants across multiple Web
    projects) is to use a custom XML file and a custom class to read/cache the
    constants. There's an article that looks at this here:

    Using XML to Share Constants Across Projects
    http://aspnet.4guysfromrolla.com/articles/121003-1.aspx

    A good background article - that looks at sharing constants and common
    functions - is available at:

    Accessing Common Code, Constants, and Functions in an ASP.NET Project
    http://aspnet.4guysfromrolla.com/articles/122403-1.aspx

    Happy Programming!

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Feb 26, 2004
    #2
    1. Advertising

  3. Hi Scott,

    First, you need to figure out what kind of scope these variables should
    have. "Global" is kind of misleading. Global to what? Global to a page,
    global to all pages of a single user Session, or global to all users in all
    pages? Second, you need to determine whether or not these values should be
    changed on the fly. If not, and if they are truly global to the entire app,
    then they should probably go into the web.config file. If they should be
    changed, you would want to store them in memory somewhere. Again, where
    depends on scope:

    1. Global to entire app and all users: Application Cache, File, or Database
    2. Global to a single user across all pages during a single user Session:
    Session
    3. Global to a single page: Property, Field, HttpContext, or ViewState

    Note: This is a bit simplified, but should give you a good starting point.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "Schoo" <-hc.com> wrote in message
    news:eCTmzNJ$...
    > I have some program constants that I need to set and I thought it would be
    > best to set them all up in a central location so that I can change them
    > quickly when we go from development to production. What is the best way

    to
    > do that? Should I put them in web.config? Should I create a special

    class
    > for these settings? Wherever it is best to write them, can you give me a
    > line or 2 of code to show the best way to set a constant string?
    >
    > Thanks,
    >
    > Scott
    >
    >
    Kevin Spencer, Feb 26, 2004
    #3
  4. Schoo

    jef Guest

    So is it bad practice to put globals in the Global.asax?
    I have some global constants in there as ReadOnly Shared Properties.

    That way when I need to use one, I just have to type:

    Global.MyPropertyName

    Seems a lot cleaner than getting values from the web.config all the time.

    What are some drawbacks of doing it this way?

    Thanks!

    jef
    jef, Feb 26, 2004
    #4
  5. Schoo

    Schoo Guest

    Thank you for your response.

    I took a look at the first link you have (didn't get to all of them), and
    seemed like the suggestion is exactly what I want. But when I tried it I
    get "Exception creating section handler". Here is my code:

    <configuration>
    <configSections>
    <section name="PatientSurveySettings"
    type="System.Configuration.NameValueFileSectionHandler,
    System, Version=1.0.3300.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089" />
    </configSections>

    <MyAppSettings>
    <add key="connString" value="connection string" />
    </MyAppSettings>
    ...
    </configuration>

    It crashes on line #3. Am I not supposed to use the exact numbers or
    something?

    Scott

    "Scott Mitchell [MVP]" <> wrote in message
    news:c1r%b.2875$...
    > > I have some program constants that I need to set and I thought it would

    > be
    > > best to set them all up in a central location so that I can change them
    > > quickly when we go from development to production. What is the best way

    > to
    > > do that? Should I put them in web.config? Should I create a special

    > class
    > > for these settings? Wherever it is best to write them, can you give me

    > a
    > > line or 2 of code to show the best way to set a constant string?

    >
    > You could put them in the Web.config's <appSettings> block, that's one
    > option (more information at
    > http://aspnet.4guysfromrolla.com/articles/053102-1.aspx). The
    > disadvantage with this is if you have multiple Web applications that need
    > to share constants you have to replicate the <appSettings> section in
    > numerous projects.
    >
    > Another option (that is useful for sharing constants across multiple Web
    > projects) is to use a custom XML file and a custom class to read/cache the
    > constants. There's an article that looks at this here:
    >
    > Using XML to Share Constants Across Projects
    > http://aspnet.4guysfromrolla.com/articles/121003-1.aspx
    >
    > A good background article - that looks at sharing constants and common
    > functions - is available at:
    >
    > Accessing Common Code, Constants, and Functions in an ASP.NET Project
    > http://aspnet.4guysfromrolla.com/articles/122403-1.aspx
    >
    > Happy Programming!
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    >
    >
    >
    >
    Schoo, Feb 27, 2004
    #5
  6. Hi Scott,

    Have you had a chance to check out the things provided in my last reply or
    have you resolved this problem? If you need any further help, please feel
    free to post here.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Mar 3, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    374
    Knute Johnson
    Oct 26, 2005
  2. Bryan Parkoff
    Replies:
    2
    Views:
    6,478
  3. Replies:
    1
    Views:
    377
    Michael Ekstrand
    Aug 21, 2005
  4. jubelbrus
    Replies:
    5
    Views:
    602
    JohnQ
    Jul 20, 2007
  5. Replies:
    10
    Views:
    749
Loading...

Share This Page