473,554 Members | 4,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on constants

Hi,

Is there any benefit to declaring variables as constants other than not
being able to overwrite them by mistake?
Thanks
Simon
Nov 15 '05 #1
6 1287
Hi,

I guess Constants get replaced with there value while compiling which helps
fast execution.

"Simon Harvey" <si**********@t he-web-works.co.uk> wrote in message
news:uK******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

Is there any benefit to declaring variables as constants other than not
being able to overwrite them by mistake?
Thanks
Simon

Nov 15 '05 #2
Beware, though: I've read elsewhere that if you define a constant (as in a
const), and then use that constant as a parameter to a method call, that the
constant value (rather than a reference to the constant) will be encoded
method call. That's no big deal if you build all of your code at once, but
if the constant is in a shared shared, and you change its value in the
future, it won't necessarily "take" for existing, possibly deployed,
assemblies that use the constant value in a method call.
"Simon Harvey" <si**********@t he-web-works.co.uk> wrote in message
news:uK******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

Is there any benefit to declaring variables as constants other than not
being able to overwrite them by mistake?
Thanks
Simon

Nov 15 '05 #3
This is true. Declaring a variable const has that implication.

In fact, every reference made to a "const" variable, will be replaced with
the value of the variable when the code is compiled. Not just method calls.

This has some reprecussions when other people depend on your code. Take the
following scenario. Company A's product depends on your dll which contains
constant definitions. They ship their product built against version 1 of
your dll. Then you ship version 2 of the dll in which some of the
constants' values have changed. Because Company A's product was compiled
against version 1 of your dll, they are still using the constant values from
version 1. This could potentially cause their app to break.

--
Jared Parsons [MSFT]
ja******@online .microsoft.com
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.c om/info/cpyright.htm"

"J.Marsch" <je****@ctcdeve loper.com> wrote in message
news:um******** ******@TK2MSFTN GP11.phx.gbl...
Beware, though: I've read elsewhere that if you define a constant (as in a const), and then use that constant as a parameter to a method call, that the constant value (rather than a reference to the constant) will be encoded
method call. That's no big deal if you build all of your code at once, but if the constant is in a shared shared, and you change its value in the
future, it won't necessarily "take" for existing, possibly deployed,
assemblies that use the constant value in a method call.
"Simon Harvey" <si**********@t he-web-works.co.uk> wrote in message
news:uK******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

Is there any benefit to declaring variables as constants other than not
being able to overwrite them by mistake?
Thanks
Simon


Nov 15 '05 #4
Jared Parsons [MSFT] <ja******@onlin e.microsoft.com > wrote:
This is true. Declaring a variable const has that implication.

In fact, every reference made to a "const" variable, will be replaced with
the value of the variable when the code is compiled. Not just method calls.

This has some reprecussions when other people depend on your code. Take the
following scenario. Company A's product depends on your dll which contains
constant definitions. They ship their product built against version 1 of
your dll. Then you ship version 2 of the dll in which some of the
constants' values have changed. Because Company A's product was compiled
against version 1 of your dll, they are still using the constant values from
version 1. This could potentially cause their app to break.


And this is why we have strong versioning, of course - Company A's
product should use version 1 of the DLL whether or not a version 2 is
also present.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Thanks all

Simon
Nov 15 '05 #6
Another alternative.

Company A use a private copy of Company B's dll. That way when a global
update occurs (say in the GAC), Company A is not broken by the changes
Company B makes to their dll.

Then again, they may also be missing security updates.

--
Jared Parsons [MSFT]
ja******@online .microsoft.com
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.c om/info/cpyright.htm"
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Jared Parsons [MSFT] <ja******@onlin e.microsoft.com > wrote:
This is true. Declaring a variable const has that implication.

In fact, every reference made to a "const" variable, will be replaced with the value of the variable when the code is compiled. Not just method calls.
This has some reprecussions when other people depend on your code. Take the following scenario. Company A's product depends on your dll which contains constant definitions. They ship their product built against version 1 of your dll. Then you ship version 2 of the dll in which some of the
constants' values have changed. Because Company A's product was compiled against version 1 of your dll, they are still using the constant values from version 1. This could potentially cause their app to break.


And this is why we have strong versioning, of course - Company A's
product should use version 1 of the DLL whether or not a version 2 is
also present.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1205
by: Sugapablo | last post by:
I'm trying to use the Const statement to define a group of constants. I would like different groups of constants, one for each language (such as english, spanish, etc.) Depending on a variable (cookie, querystring, etc.) I would like the constants to be defined differently. One thing I tried was:...
4
2558
by: gabriel | last post by:
Greetings, I am working on a project and cannot choose the best way to achieve this : I got a method which returns an error code like this : DISK_FULL or PERMISSION_DENIED. Those are constants defined in a class named "Constants" : public static byte DISK_FULL = 1;
13
3045
by: poison.summer | last post by:
Hello If I have an enum definition for instance typedef enum { F0 = 0, F1 = 1, F2 = 2, F3 =3
2
1813
by: yawnmoth | last post by:
The PHP license states the following: 4. Products derived from this software may not be called "PHP", nor may "PHP" appear in their name, without prior written permission from group@php.net. You may indicate that your software works in conjunction with PHP by saying "Foo for PHP" instead of calling it "PHP Foo" or "phpfoo" As the author...
4
2003
by: D. Yates | last post by:
Hi all, Is there a place in the .NET framework where simple time constants are defined? For example: How many days are there in a week? Hours in a day? Minutes in an hour? Seconds in a minute? None of these are going to change, so they are obviously constants. Now I
10
1562
by: Steven W. Orr | last post by:
I saw this and tried to use it: ------------------><8------------------- const.py------------- class _const: class ConstError(TypeError): pass def __setattr__(self,name,value): if self.__dict__.has_key(name): raise self.ConstError, "Can't rebind const(%s)"%name self.__dict__=value
3
1250
by: Steven W. Orr | last post by:
This is all an intro learning experience for me, so please feel free to explain why what I'm trying to do is not a good idea. In the Cookbook, they have a recipe for how to create global constants. ----------------- class _const: class ConstError(TypeError): pass def __setattr__(self,name,value): if self.__dict__.has_key(name):
6
1623
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...
3
1673
by: Microsoft | last post by:
Hi I have a c# program that continually runs 24/7 and performs a variety of tasks based on a timer. There is one routine that hangs every Saturday morning without fail. If I restart the program throughout the week is still hangs. I can restart it immediatly after the hang and it runs fine until next Saturday. There is nothing different...
54
3558
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header file writing: const double PI = 3.14; Every time using it, include the header file.
0
7506
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
8018
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7541
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6123
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
5423
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
5142
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
3533
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2006
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
823
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.