Global Constants?

G

Greg Smith

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

Hans Kesting

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
 
G

Greg Smith

ACK!

I think I have "dain bramage" today.

What I meant was a global #define value.

Can that be done?
 
J

Jon Skeet [C# MVP]

Greg Smith said:
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?
 
G

Greg Smith

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

Jon Skeet [C# MVP]

Greg Smith said:
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.)
 
O

Otis Mukinfus

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
 

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

Top