473,805 Members | 1,958 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 1296
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
1217
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
2568
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
3067
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
1840
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 of something released under the PHP license, can I, as I see fit, just grant permission to an...
4
2023
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
1585
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
1263
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
1635
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 there a way to make the variables unmodifiable in config.php so that scripts that use them ,
3
1688
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 about Saturday that I can think of.
54
3627
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
9716
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10607
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10364
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10104
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7645
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
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

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.