| << 4.4.0- Conversions | Chapter4 | 4.5.1- VBScript Constants >> |
Constants
There will be occasions when you want a value assigned to avariable to remain constant throughout the execution of the code. A goodexample is statewide sales tax, this value will rarely, if ever, change, yetwhen calculating the total of a shopping basket, you'll probably need to referto it several times. Even if the tax is changed, you'd still only need to referto one value, and you'd only need to update one value. To represent the salestax you can you use something other than a variable, namely a constant.
Constants are likevariables except that, once they have been assigned a value, they don't change.Many programming languages provide an explicit facility for constants, byallowing the programmer to assign an initial value to the constant, andsubsequently forbidding any alteration of that value. The main reason you'dassign a value to a constant is to prevent accidental alteration of that value.
VBScript supports constants with the Const keyword being used to define them. By convention, constantsare named in upper case:
Const ABSOLUTEZERO = -273
If you tried then to assign another value to ABSOLUTEZERO, such as:
ABSOLUTEZERO = 0
The change would be rejected and an error message would beproduced. Constants remain in force for the duration of the script, just likevariables do, but no longer. While it isn't possible to amend their value onceit has been set, it is possible to set them to something different in the page,when you run a page again. Constants make your code easier to read andmaintain, as they require less updating and if you choose a self-explanatoryname, then they make your code easily understandable.
| << 4.4.0- Conversions | Chapter4 | 4.5.1- VBScript Constants >> |

RSS
