Best place to set a global variable

S

Schoo

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
 
S

Scott Mitchell [MVP]

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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
K

Kevin Spencer

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

jef

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
 
S

Schoo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top