471,348 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Best place for global constants

If I have some constants I want to use throughout my application, how should
I implement that?

const string USA = "US"
const string CANADA = "CA"

etc.
Feb 4 '06 #1
4 1515
"Dave" <Da**@discussions.microsoft.com> a écrit dans le message de news:
41**********************************@microsoft.com...

| If I have some constants I want to use throughout my application, how
should
| I implement that?
|
| const string USA = "US"
| const string CANADA = "CA"

Place them in a static class in the application's namespace.

public static class Consts
{
public static readonly string US = "USA";

public static readonly string CANADA = "CA";
}

Then you address them like this :

{
string test = Consts.US;
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 4 '06 #2
Dave <Da**@discussions.microsoft.com> wrote:
If I have some constants I want to use throughout my application, how should
I implement that?

const string USA = "US"
const string CANADA = "CA"


Place them in a class which describes the kind of constants they are.
For instance, in this case it might be CountryCodes:

public static class CountryCodes
{
public const string Usa = "US";
public const string Canada = "CA";
}

(The "static class" bit is .NET 2.0 - for .NET 1.1 just add a private
constructor to make sure the class isn't instantiated. Not that it
really matters if it is...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 4 '06 #3
SJ
Peter,
Why should the Global Class inherit System.Web.HttpApplication ?
"Peter Bromberg [C# MVP]" wrote:
public class Global : System.Web.HttpApplication
{
public const string USA="US";
in your page:

private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text=Global.USA;


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Dave" wrote:
If I have some constants I want to use throughout my application, how should
I implement that?

const string USA = "US"
const string CANADA = "CA"

etc.

Feb 4 '06 #4
public class Global : System.Web.HttpApplication
{
public const string USA="US";
in your page:

private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text=Global.USA;


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Dave" wrote:
If I have some constants I want to use throughout my application, how should
I implement that?

const string USA = "US"
const string CANADA = "CA"

etc.

Feb 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by Daniel Bass | last post: by
4 posts views Thread by Amadelle | last post: by
5 posts views Thread by Schoo | last post: by
1 post views Thread by 2obvious | last post: by
6 posts views Thread by lazy | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.