Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Best place to set a global variable

Reply
Thread Tools

Best place to set a global variable

 
 
Schoo
Guest
Posts: n/a
 
      02-26-2004
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


 
Reply With Quote
 
 
 
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      02-26-2004
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

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




 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      02-26-2004
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" <(E-Mail Removed)-hc.com> wrote in message
news:eCTmzNJ$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
jef
Guest
Posts: n/a
 
      02-26-2004
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
 
Reply With Quote
 
Schoo
Guest
Posts: n/a
 
      02-27-2004
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]" <(E-Mail Removed)> wrote in message
news:c1r%b.2875$(E-Mail Removed) 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
> (E-Mail Removed)
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!
>
>
>
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      03-03-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best place for "Global Variables" and Methods in C# though ASP.Net Bub.Paulson@gmail.com ASP .Net 10 11-08-2007 10:39 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Is mymodule.myvariable a 'global'? (What is proper definition of 'global variable'?) seberino@spawar.navy.mil Python 1 08-21-2005 09:21 PM
Use Global Variable or Global Struct??? Bryan Parkoff C++ 2 11-27-2004 02:46 AM



Advertisments