By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

Best place to set a global variable

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> 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/art...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
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

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


Nov 18 '05 #2

P: n/a
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" <sc***********@nospam.ma-hc.com> wrote in message
news:eC**************@TK2MSFTNGP09.phx.gbl...
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

Nov 18 '05 #3

P: n/a
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
Nov 18 '05 #4

P: n/a
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.NameValueFileSectionHan dler,
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]" <mi******@4guysfromrolla.com> wrote in message
news:c1*****************@newssvr27.news.prodigy.co m...
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/art...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
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

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

Nov 18 '05 #5

P: n/a
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

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.