473,320 Members | 2,112 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,320 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 12367
> 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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.