473,548 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12379
> 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.THEVALU E 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.co m...
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.THEVALU E 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.co m>
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.co m>
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
4341
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 doing it - is it not? Once the application has loaded the page it is cached and is immediately available for other pages. With global variables -...
10
17837
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 overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if...
20
4452
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 structurally sound to use Application_OnStart. Am I attempting the impossible, and if so, why?
1
2746
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 colors are referred in different subsystems in our projects. They are defined as enumeration constants and a single color must be the same value...
25
66043
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 layers/objects/etc... or does it expect me to create a singleton global class structure? surely it's not that terrible.
4
51158
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 a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any...
8
2875
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 class that I want its instance to be accessible from any other classe in the project. There is also a bunch of constants that I want to be public...
1
3013
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 do it this way, the scope of the constants is local--attempting to use these constants in a typical .aspx file throws an error telling me that...
8
5669
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 without having to jump through a bunch of hoops. First, let me layout how it worked in 1.1. In the Global.asax, within the Global class construct, I...
6
1622
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 including config.php and declaring the variables that will be used as global. This seems very inefficient. 2.Moreover these variables are constants, is...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.