471,318 Members | 3,268 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Global Constants?

Is it possible to declare a global constant?

I would like to have a constant visible throughout the application but I
can't get the syntax correct to make it global.

Any help is greatly appreciated.


Feb 10 '06 #1
6 12299
> Is it possible to declare a global constant?

I would like to have a constant visible throughout the application but I
can't get the syntax correct to make it global.

Any help is greatly appreciated.


If you have

public class Library
{
public const int THEVALUE = 1;
}

you can use Library.THEVALUE where you need it.
I don't think you can skip the "Library." part.

Hans Kesting
Feb 10 '06 #2
ACK!

I think I have "dain bramage" today.

What I meant was a global #define value.

Can that be done?

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:mn***********************@spamgourmet.com...
Is it possible to declare a global constant?

I would like to have a constant visible throughout the application but I
can't get the syntax correct to make it global.

Any help is greatly appreciated.


If you have

public class Library
{
public const int THEVALUE = 1;
}

you can use Library.THEVALUE where you need it.
I don't think you can skip the "Library." part.

Hans Kesting

Feb 10 '06 #3
Greg Smith <gj*@umn.edu> wrote:
ACK!

I think I have "dain bramage" today.

What I meant was a global #define value.

Can that be done?


No. You can set pre-processor symbols which are defined throughout the
project (in VS.NET at least) but those don't have values.

Why do you not want to use "const" as already suggested?

--
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 10 '06 #4
>> Can that be done?

No. You can set pre-processor symbols which are defined throughout the
project (in VS.NET at least) but those don't have values.

Why do you not want to use "const" as already suggested?


I started this whole thing out poorly. I want to do conditional compiles
and my concern is the scope on the #define.
Feb 20 '06 #5
Greg Smith <gj*@umn.edu> wrote:
No. You can set pre-processor symbols which are defined throughout the
project (in VS.NET at least) but those don't have values.

Why do you not want to use "const" as already suggested?


I started this whole thing out poorly. I want to do conditional compiles
and my concern is the scope on the #define.


In that case, you need to use the project options to define/undefine
preprocessor symbols. You can't do it in source for anything other than
that particular source file.

(This is a bit of a pain occasionally.)

--
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 20 '06 #6
On Mon, 20 Feb 2006 12:38:05 -0600, "Greg Smith" <gj*@umn.edu> wrote:
Can that be done?


No. You can set pre-processor symbols which are defined throughout the
project (in VS.NET at least) but those don't have values.

Why do you not want to use "const" as already suggested?


I started this whole thing out poorly. I want to do conditional compiles
and my concern is the scope on the #define.

It is project wide. If you look in the Vs 2005 Project/<yourproject> Properties
menu and click the Build tab, you will see two check boxes for TRACE and DEBUG
settings. Checking these two checkboxes (the default) defines the two
throughout your project.

If you are developing on one machine and deploying to others you can use this
mechanism to change settings such as connection strings as the pseudo code below
shows.

#if DEBUG
Get the local machine connection string from the config file.
#else
Get the connection string of the remote server from the config file
#endif

In fact VS 2005 and VS 2003 will dim the text as appropriate to show you the one
the build will use.

When you set the configuration of the build to Release Debug and Trace will no
longer be defined and the release connection will be the one your application
will get from the config file.

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Feb 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Matt | last post: by
20 posts views Thread by 2obvious | last post: by
25 posts views Thread by Daniel Bass | last post: by
4 posts views Thread by Amadelle | last post: by
8 posts views Thread by Marty | last post: by
1 post views Thread by 2obvious | last post: by
8 posts views Thread by Thomas Coleman | 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.