473,241 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 12363
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
20
by: 2obvious | last post by:
I've been trying to create read-only global variables by creating constants (Const) in my global.asa, but I can't seem to reference them. Sticking them in an include works fine, but it seems more...
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
8
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
1
by: 2obvious | last post by:
I want to declare some constants on the application level in global.asax to use throughout my application, e.g.: Sub Application_OnStart() Const NUM As Integer = 5 End Sub Problem is, when I...
8
by: Thomas Coleman | last post by:
Ok, I've obviously discovered that Global.aspx has been completely changed in ..NET 2.0. However, I haven't figured out how to declare a constant that's available to any page in my application...
6
by: lazy | last post by:
hi, I have some constants defined in a php script say config.php. I want to use the variables there defined in other scripts. couple of questions regd that: 1. Is there an alternative to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.